2

I am using Feedzirra and am trying to sort a hash of feed data by the published date. I'm not getting an error or anything, it just isn't sorting. Here's the code in my controller:

class ClassifiedsController < ApplicationController
  def index
    require 'feedzirra'

    feed_urls = ["http://newjersey.craigslist.org/jjj/index.rss", "http://cnj.craigslist.org/jjj/index.rss"]
    feeds = Feedzirra::Feed.fetch_and_parse(feed_urls)

    x = 1
    @items = Hash.new

    feeds.each do |feed_url, feed|

      feed.entries.each do |entry|
        @items[x] = {:title => entry.title, :url => entry.url,
                     :published => entry.published.to_i}
        x = x + 1
      end

    end

    @items.sort_by { |k, v| v[:published] }
  end
end

Any help would be greatly appreciated. Thanks!

Danny I.
  • 57
  • 5

1 Answers1

0

In Ruby Hash does not preserve item order, You can't rely on it. Alternatively build an array of two dimensional arrays to preserve order:

[ 
  ["item1", "value1"],
  ["item2", "value2"]
]
Edgars Jekabsons
  • 2,833
  • 15
  • 20
  • I actually ended up using a database for that project, but used your method successfully on another project. Thanks for the help! – Danny I. Nov 03 '13 at 05:24