Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
This is my input hash:
h = [ {user_id: 1, bookings_nd: 3}, {user_id: 2, bookings_nd: 10}, {user_id: 3, bookings_nd: 2} ]
I need the result to be sorted in descending order of 'bookings_nd' rather than 'user_id'. I want it to look like this:
h = [ {user_id: 2, bookings_nd: 10}, {user_id: 1, bookings_nd: 3}, {user_id: 3, bookings_nd: 2} ]
How to do it?
h
You can do
h.sort_by! { |k| -k[:bookings_nd] }
or
h.sort_by! { |k| k[:bookings_nd] }.reverse!
Also i guess this question is duplicate for Sorting an array in descending order in Ruby
Add a comment
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
hsince this is usually reserved for hashes.