How to sort a Ruby Hash by number value?

Dustin M. picture Dustin M. · Mar 29, 2010 · Viewed 121.9k times · Source

I have a counter hash that I am trying to sort by count. The problem I am running into is that the default Hash.sort function sorts numbers like strings rather than by number size.

i.e. Given Hash:

metrics = {"sitea.com" => 745, "siteb.com" => 9, "sitec.com" => 10 }

Running this code:

metrics.sort {|a1,a2| a2[1]<=>a1[1]}

will return a sorted array:

[ 'siteb.com', 9, 'sitea.com', 745, 'sitec.com', 10]

Even though 745 is a larger number than 9, 9 will appear first in the list. When trying to show who has the top count, this is making my life difficult. :)

Any ideas on how to sort a hash (or an array even) by number value size?

I appreciate any help.

Answer

Marc-Andr&#233; Lafortune picture Marc-André Lafortune · Mar 29, 2010

No idea how you got your results, since it would not sort by string value... You should reverse a1 and a2 in your example

Best way in any case (as per Mladen) is:

metrics = {"sitea.com" => 745, "siteb.com" => 9, "sitec.com" => 10 }
metrics.sort_by {|_key, value| value}
  # ==> [["siteb.com", 9], ["sitec.com", 10], ["sitea.com", 745]]

If you need a hash as a result, you can use to_h (in Ruby 2.0+)

metrics.sort_by {|_key, value| value}.to_h
  # ==> {"siteb.com" => 9, "sitec.com" => 10, "sitea.com", 745}