My data is in the form of:
a = [
{
"a_id":101,
"a_value":100000.0,
"a_quantity":360.0
},
{
"a_id":108,
"a_value":110000.0,
"a_quantity":210.0
},
{
"a_id":104,
"a_value":105000.0,
"a_quantity":310.0
}
]
I would like the data to be sorted in descending order of a_value
. I have tried:
a.sort_by {|k| k[:a_value] }.reverse
But it does not get sorted.
What you have works. Just don't forget to assign the sorted collection to a variable (sort_by
and reverse
do not change the collection).
Bonus: here's arguably a nicer version (one pass, instead of two)
a.sort_by{ |v| -v[:a_value] }