Mega Code Archive

 
Categories / Ruby / Hash
 

Sort a hash

myHash = { 1 => "One", 2 => "Two", 3 => "Three", 4 => "Four", 5 => "Five" } p myHash # => {5=>"Five", 1=>"One", 2=>"Two", 3=>"Three", 4=>"Four"} myHash.sort # => [[1, "One"], [2, "Two"], [3, "Three"], [4, "Four"], [5, "Five"]] p myHash