Sorting an Array of Hashes with unknown keys

Posted by root Sun, 20 Jul 2008 03:46:00 GMT

AoH = [{"z"=>26}, {"g"=>7}, {"r"=>18}, {"a"=>1}, {"v"=>22}]

# sort by key
AoH.map{|e| e.to_a.flatten}.sort{|x,y| x[0] <=> y[0]}
# sort by value
AoH.map{|e| e.to_a.flatten}.sort{|x,y| x[1] <=> y[1]}

Posted in  | Tags  | no comments

Comments

Comments are disabled