2013-10-29 27 views
2

我正在使用Feedzirra,並試圖根據發佈日期對源數據進行哈希排序。我沒有收到任何錯誤,只是沒有排序。以下是我的控制器中的代碼:Feedzirra和哈希分類

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 

任何幫助將不勝感激。謝謝!

+0

這裏看一看:[誘騙排序在紅寶石哈希(http://stackoverflow.com/a/12833824/2312749) – Pol0nium

回答

0

在紅寶石哈希不保留項目順序,你不能依靠它。 可選地建立二維陣列的陣列,以維持秩序:

[ 
    ["item1", "value1"], 
    ["item2", "value2"] 
] 
+0

我實際上最終使用該項目的數據庫,但在另一個項目上成功使用了您的方法。謝謝您的幫助! –