I have an array of hash like this:
[{683=>5}, {689=>2}, {692=>10}]
I want the result like this:
[{692=>10}, {683=>5}, {689=>2}]
Could any one help me?
Use Enumerable#sort_by. The return value of the block is used as comparison key.
[{683=>5}, {689=>2}, {692=>10}].sort_by { |h| -h.values[0] }
# => [{692=>10}, {683=>5}, {689=>2}]
sort_by { |h| h.values[0] }.reverse!, surprisingly it is slighlty faster and a little clearer, IMHO.reverse! take O(n) times. But decorate is performed in ruby loop, while reverse! is performed in C. More operations in the block means caused visible time difference.