我正在使用rails 2.我手动创建了具有不同活动记录值的数组,我想通过created_at对数组进行排序,但没有得到任何线索,我被卡住了。请帮忙
这是我的代码:
@player_ranking_details[:qb].each do |nfd|
@new_nfd << nfd.player.news_breakers.last
end
并输出:
[#<NewsBreaker id: 4562, player_id: 981, title: "Newsfeed for week 6", source: "http://pyromaniac.com", active: true, publish_time: "2017-05-25 06:05:00", feature: false, sticky: false, created_at: "2017-05-25 06:06:38", updated_at: "2017-08-09 12:53:41">, #<NewsBreaker id: 3361, player_id: 187, , title: "Troy Polamalu ruled out for Week 15", source: "http://twitter.com/jimwexell/status/6701947444", active: true, publish_time: "2009-12-15 18:00:00", feature: true, sticky: false, created_at: "2009-12-15 20:33:46", updated_at: "2009-12-15 20:33:46">...]
有没有办法按照desc顺序对数组ny created_at中的记录进行排序。
答案 0 :(得分:1)
您可以随时使用属性@new_nfd.sort_by { |i| i.created_at }
对于降序,reverse
以上结果。