Last Updated: February 25, 2016
·
393
· mejarc

Sort an array of hashes by given key

sorted = array.sort {|a, b| a[key] <=> b[key]}

Note that this is different from:

sorted_symbol = array.sort{ |a, b| a[:key] <=> b[:key]}

1 Response
Add your response

sorted = array.sort_by { |x| x[key] }

over 1 year ago ·