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.

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. :-)


---
demerphq

    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

    Make the inlined sort routine take three args like this:

    This suggestion is effectively equivalent to mine. I just applied what is essentially currying to simplify things.

    Makeshifts last the longest.

      Well actually you hadn't posted the updated version when I posted that. Your original wouldn't work if the sort function was declared in a different package scope. (As I suspected and as you realized :-)


      ---
      demerphq

        First they ignore you, then they laugh at you, then they fight you, then you win.
        -- Gandhi