in reply to RE: Sort Array of Hashes by values of multiple hash keys.
in thread Sort Array of Hashes by values of multiple hash keys.
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"; }
|
|---|