in reply to sorting an array of hashes by values but ...
It takes a bit of thought to figure it out, but once you do, you'll have scary Perl kung-fu. The trick is adding extra data, sorting on that extra data, then discarding the extra data.my @hashes = ( { key1 => 1, key2 => 4 }, { key3 => 8, key4 => 4, key5 => 6 }, { key6 => 3 }, ); sub sum { my $sum; $sum += $_ for (@_); return $sum; } @hashes = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [ $_, sum( values %$_) ] } @hashes; use Data::Dumper; print Dumper(\@hashes);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: sorting an array of hashes by values but ...
by slayven (Pilgrim) on Feb 08, 2001 at 23:19 UTC |