0

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

4
  • 2
    What have you tried already, possibly after having a look at the various sort methods of arrays? Please show us the code. What were the issues with your approach? Commented Aug 6, 2014 at 13:48
  • why are you calling :datetime Commented Aug 6, 2014 at 13:52
  • You have an array of arrays, so do something like sort_by {|x| x.first[:start_date]} Commented Aug 6, 2014 at 13:56
  • @Grych am getting this error Symbol as array index - (TypeError) Commented Aug 6, 2014 at 13:58

1 Answer 1

3

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

arr.sort_by {|vn| vn[0]["calendar"][:start_date]}
Sign up to request clarification or add additional context in comments.

2 Comments

Your old answer only working... arr.sort_by {|vn| vn["calendar"][:start_date]}
In your question, you have an array of arrays. If my previous answer works, then you probably have an array of hashes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.