E
E
eldar_web2016-06-23 18:39:17
Ruby on Rails
eldar_web, 2016-06-23 18:39:17

How to sort a hash by one element in Ruby?

For example, there is a hash:

list = { :name => { 1 => 'Rafaello', 2 => 'Antonello', 3 => 'Sergeello' }, :count => { 1 => 500, 2=> 700, 3=>100 } }

How to sort by count (descending and vice versa) ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Demidenko, 2016-06-24
@eldar_web

Same question again

arr =[{ :name => 'Rafaello', :count => 500 }, { :name => 'Antonello', :count => 700 }, { :name => 'Sergeello', :count => 100 } ]
arr.sort_by {|v| v[:count] }
arr.sort_by {|v| v[:count] }.reverse

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question