189

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.

1
  • what ruby version do you use? your sort result is very strange
    – fl00r
    Mar 29, 2010 at 19:39

4 Answers 4

319

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}
15
  • 91
    or simply sort_by{|k,v| v} Mar 29, 2010 at 19:01
  • My number was returning as a string, that fixed it.. I had a2 and a1 in that order because I wanted the results to sort decending.. thanks for your feedback though.
    – Dustin M.
    Mar 29, 2010 at 22:18
  • 22
    @Elchin: you can use metrics.sort_by{ |k, v| v }.reverse.to_h Jan 13, 2015 at 8:04
  • 4
    Actually even simpler as: hash.sort_by(&:last) with the same caveat about getting an array of pairs vs. a Hash. May 18, 2015 at 13:55
  • 3
    for reverse sort you may use metrics.sort_by{|k,v| -v}
    – bragboy
    Nov 2, 2017 at 6:28
104

Since value is the last entry, you can do:

metrics.sort_by(&:last)
2
15

Already answered but still. Change your code to:

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

Converted to strings along the way or not, this will do the job.

11

That's not the behavior I'm seeing:

irb(main):001:0> metrics = {"sitea.com" => 745, "siteb.com" => 9, "sitec.com" =>
 10 }
=> {"siteb.com"=>9, "sitec.com"=>10, "sitea.com"=>745}
irb(main):002:0> metrics.sort {|a1,a2| a2[1]<=>a1[1]}
=> [["sitea.com", 745], ["sitec.com", 10], ["siteb.com", 9]]

Is it possible that somewhere along the line your numbers are being converted to strings? Is there more code you're not posting?

2
  • Ahh your right it looks like the result in my code was returning it as a string. Pesky data types. :) Sometimes I am just too close to the problem. Thanks.
    – Dustin M.
    Mar 29, 2010 at 22:18
  • 2
    Yup. Occasionally I hear someone refer to Ruby as "untyped". Oh, no, it's definitely typed. It's just not statically typed. :) Mar 30, 2010 at 1:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.