I was trying to sort a particular hash by values. I came across a way using the method sort_by
. But even though I call sort_by
on a hash, it returns an array, i.e.:
a = {}
a[0] = "c"
a[1] = "b"
a[2] = "a"
a.sort_by{|key, value| value}
# => [[2, "a"], [1, "b"], [0, "c"]]
If I try to convert the result into a hash, I end up with a hash sorted on key, hence the whole purpose of sort_by
is lost. i.e.:
a = {}
a[0] = "c"
a[1] = "b"
a[2] = "a"
Hash[*a.sort_by{|key, value| value}.flatten]
# => {0=>"c", 1=>"b", 2=>"a"}
Is there a way I can sort a hash by value and yet get the results back in the form of a Hash?
I am using 1.8.6 ruby