in reply to Sort Array of Hashes by values of multiple hash keys.

Unless I'm missing your point. Sorting an array of hashes by the values of multiple keys is easy:
my @l=( {FN=>'Les',LN=>'Howard'}, {FN=>'Larry',LN=>'Wall'}, {FN=>'Randal',LN=>'Schwartz'}); foreach(sort {$$a{LN} cmp $$b{LN} or $$a{FN} cmp $$b{FN}} @l){ print "$$_{LN}, $$_{FN}\n"; }
The above code sorts a lits of hashes (each hash containing first and last names) by last name and then first name. The order of the original @l is not altered.

Replies are listed 'Best First'.
RE: Sort Array of Hashes by values of multiple hash keys.
by davorg (Chancellor) on Jun 21, 2000 at 13:04 UTC

    This works, of course, but on a long list the overhead of doing all of those hash lookups multiple times may start to effect performance.

    This would, therefore, be a great time to try out a Schwartzian Transform - which would look something like this:

    my @l = ({FN => 'Les', LN => 'Howard'}, {FN => 'Larry', LN => 'Wall'}, {FN => 'Randal', LN => 'Schwartz'}); my @sorted = map { $_->[2] } sort { $a->[1] cmp $b->[1] || $a->[0] cmp $b->[0] } map { [$_->{FN}, $_->{LN}, $_] } @l; foreach (@sorted) { print "$_->{LN}, $_->{FN}\n"; }

    --
    http://www.dave.org.uk

    European Perl Conference - Sept 22/24 2000
    http://www.yapc.org/Europe/
RE: RE: Sort Array of Hashes by values of multiple hash keys.
by raflach (Pilgrim) on Jun 19, 2000 at 19:49 UTC
    Where were you when I needed this easy solution originally. Well done. (smacks himself upside the head.) Didn't think of using or... (nor did anyone else! :)