Sorting an array of hashes

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
···

-----Original Message-----
From: Reimer Behrends
To: ruby-talk@ruby-lang.org
Sent: 11/10/2002 11:35 AM
Subject: Re: Sorting an array of hashes

==========================================

Hi,

I like that - after taking it apart and figuring out how it worked. I’m
using 1.6.5 but looking at the simplicity of the 1.7 method is giving me
some impetus to upgrade.

regards,

Martin