syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

Question:
In Ruby how do I sort an array of hashes?

Let Ruby do the talking:
hash1 = {:k1 => 1.19, :k2 => 2.2}
hash2 = {:k1 => 1.11, :k2 => -2.2}
hash3 = {:k1 => 11.1, :k2 => 0.1}
myarray = [hash1,hash2,hash3]
sorted_array1 = myarray.sort_by{|hsh| hsh[:k1]}
sorted_array2 = myarray.sort_by{|hsh| hsh[:k2]}

syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me