对数组中的嵌套哈希进行排序

时间:2014-08-06 13:41:44

标签: ruby-on-rails ruby arrays sorting

我有这样的样本数据:

[
  [{"calendar"=>{:start_date=>Thu, 07 Aug 2014, :title=>"Recurring Event Test", :has_downloads=>false, :description=>"<p>Recurring content</p>\r\n", :location=>"Lunch hall", :id=>243, :end_date=>Thu, 07 Aug 2014}}],
  [{"calendar"=>{:start_date=>Wed, 06 Aug 2014, :title=>"Single event", :has_downloads=>false, :description=>"<p>for date 6th</p>\r\n", :location=>"chennai", :id=>253, :end_date=>Wed, 06 Aug 2014}}] 
]

需要start_date字段排序此值。

我试过这个

sort_by {|vn| vn[:start_date]} 

显示错误nil class

1 个答案:

答案 0 :(得分:3)

vn是一个数组,start_date嵌套在calendar内。你应该做

arr.sort_by {|vn| vn[0]["calendar"][:start_date]}