in reply to sort direction

Accoring to the documentation for sort, it expects a function that returns -1, 0, or 1 for (a<b),(a==b),(a>b), respectively. So, that's all your sub needs to do.

# note passing your sub $a and $b foreach my $id (sort { mysort($direction,$a,$b) } keys %hash) { print "$id - $hash{$id}{$orderby}\n"; } # end-foreach sub mysort { my ($direction, $_a, $_b) = @_; my $result = $hash{$_a}{$orderby} <=> $hash{$_b}{$orderby} || $hash{$_a}{$orderby} cmp $hash{$_b}{$orderby}; # default to ascending, but if the dir is 'DESC', we # return the *opposite* answer return ($direction eq 'DESC') ? -($result) : $result; }

Untested, of course, but the idea should be basically sound. If I understand what you're trying to accomplish, though, you might take a different approach.

# we pass the sub the actual values to compare foreach my $id ( sort { mysort($direction,$hash{$a}{$orderby},$hash{$b}{$orderby}) } +keys %hash ) { print "$id - $hash{$id}{$orderby}\n"; } # end-foreach sub mysort { my ($direction, $_a, $_b) = @_; # generalized, simplified comparison my $result = $_a <=> $_b || $_a cmp $_b; # default to ascending, but if the dir is 'DESC', we # return the *opposite* answer return ($direction eq 'DESC') ? -($result) : $result; }
<-radiant.matrix->
A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
"In any sufficiently large group of people, most are idiots" - Kaa's Law

Replies are listed 'Best First'.
Re^2: sort direction
by Roger_B (Scribe) on Oct 07, 2005 at 22:30 UTC
    Accoring to the documentation for sort, it expects a function that returns -1, 0, or 1 for (a<b),(a==b),(a>b), respectively. So, that's all your sub needs to do.
    Actually, according to your link:
    SUBNAME ... gives the name of a subroutine that returns an integer less than, equal to, or greater than 0
    which is rather less stringent than requiring -1, 0 or 1.

    Roger

      You are correct, if pedantic. :-) Just out of curiosity, under what circumstances would it be easier or faster to return something other than -1,0,1 ?

      <-radiant.matrix->
      A collection of thoughts and links from the minds of geeks
      The Code that can be seen is not the true Code
      "In any sufficiently large group of people, most are idiots" - Kaa's Law

        Well, using sort{$b-$a} @numbers rather than sort{$b<=>$a} @numbers seems to run quite a bit more quickly and produce the desired result:

        perl> cmpthese -1, { '<=>' => q[@s1=sort{$b<=>$a} 1..10000], '-' => q[@s2=sort{$b - $a} 1..10000] };; Rate <=> - <=> 33.8/s -- -82% - 183/s 441% --

        There are probably good reasons why this shoudln't be done outside of golf though.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.