Sorting a Ruby hash by key or value.

by key

h.sort

by value

h.sort_by {|k,v| v}

Note both forms return an array of key-value pairs (i.e., an array of arrays).

Tagged ruby and dev.

 

This page was generated at 4:16 PM on 26 Feb 2018.
Copyright © 1999 - 2018 Rodney Waldhoff.