in reply to Sort by value - new twist
Update: The following is an example of a hasty post that was written without properly consider either the question or the other replies. It only answers the general case and not shemp's question. Ive reanswered below with something slightly more useful.
Make the inlined sort routine take two args like this:
my $sorter=sub { $_[0] <=> $_[1] };
and then call it like this:
my @sorted=sort { $sorter->($a,$b) } @unsorted;
At least thats what I would do. If that was what actually needed doing.
Heres what i should have said:
Make the inlined sort routine take three args like this:
my $sorter=sub { $_[0]->{$_[1]} <=> $_[0]->{$_[2]} };
and then call it like this:
my @sorted=sort { $sorter->($hashref,$a,$b) } @unsorted;
I have a feeling that the answers that use $a and $b will have problems when the $sorter is defined in a different package. Im not sure about that thou. Anyway in my own code id probably write it as above both to be simple and to avoid having to hunt down the docs on the scoping rules to be sure. :-)
First they ignore you, then they laugh at you, then they fight you, then you win.
-- Gandhi
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sort by value - new twist
by Aristotle (Chancellor) on Sep 01, 2004 at 21:38 UTC | |
by demerphq (Chancellor) on Sep 02, 2004 at 05:44 UTC |