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 9:56 PM on 15 Jan 2016.
Copyright © 1999 - 2016 Rodney Waldhoff.