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!
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: