Raj Adroit
Raj Adroit

Reputation: 3878

Sort Nested Hash Inside Array

I have sample data like this:

[
  [{"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}}] 
]

Need to sort this values by start_date field.

I tried like this

sort_by {|vn| vn[:start_date]} 

its showing error nil class

Upvotes: 0

Views: 220

Answers (1)

Santhosh
Santhosh

Reputation: 29124

vn is an Array and start_date is nested inside calendar. You should do

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

Upvotes: 3

Related Questions