2

我正在使用 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

任何帮助将不胜感激。谢谢!

4

1 回答 1

0

在 Ruby Hash 中不保留项目顺序,您不能依赖它。或者构建一个二维数组的数组以保持顺序:

[ 
  ["item1", "value1"],
  ["item2", "value2"]
]
于 2013-10-29T15:06:17.917 回答