Sort a hash : sort « Hash « Ruby






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

 








Related examples in the same category

1.When you sort a hash with the sort method, you get a multidimensional array of two-element arrays in return.