In article <C1126400B227D411AC1200A00CC456B5015510E0 / emailserver.massive.com.au>, Martin Stannard wrote: > Hi, > > I've an array of hashes I want to sort, first by key1 and then subsort by > key2. I've some code that works but was wondering if there is a more > idiomatic method of doing this in Ruby: > > # 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 # in Ruby 1.7.x finalArray = unsorted.sort_by {|e| [e['firstKey'], e['secondKey']]} > # the result > finalArray.each { |hash| puts "firstKey [#{hash["firstKey"].to_s.rjust(4)}] > secondKey [#{hash["secondKey"].to_s.rjust(4)}]" } Hope this helps, Mike -- mike / stok.co.uk | The "`Stok' disclaimers" apply. http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA mike / exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60 http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA