Answer the question
In order to leave comments, you need to log in
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 } }
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question