From: Reimer Behrends Date: 2002-10-11T10:35:47+09:00 Subject: Re: Sorting an array of hashes Martin Stannard (martin@massive.com.au) wrote: [...] > # create an array of Hashes. We want to sort by firstKey, then secondKey. > unsorted = [] > 100.times do |t| > unsorted.push({ "firstKey" => rand(10), "secondKey" => t}) > end Since all you seem to need is a lexicographic ordering, how about: unsorted.map{|h|[h["firstKey"],h["secondKey"],h.id,h]}.sort.map{|_,_,_,h|h} or, in 1.7: unsorted.sort_by{|h|[h["firstKey"],h["secondKey"]]} The h.id in the first case is necessary to prevent the individual hashes from being compared, which doesn't work. In either case, we punt by relying on the fact that <=> on arrays implements lexicographic ordering. [...] Reimer Behrends