in reply to sort direction

It's worth mentioning that reversing the result of the sort isn't quite the same as having the sort function return the opposite. For instance:
$ perl -e'print sort { $b <=> $a } qw/0a 0b/' 0a0b $ perl -e'print reverse sort { $a <=> $b } qw/0a 0b/' 0b0a

Replies are listed 'Best First'.
Re^2: sort direction
by BrowserUk (Patriarch) on Oct 09, 2005 at 23:52 UTC

    With the sort as shown by the OP it will

    P:\test>perl -e"print sort { $b <=> $a || $b cmp $a } qw/0a 0b 1a 1b/" 1b1a0b0a P:\test>perl -e"print reverse sort { $a <=> $b || $a cmp $b } qw/0a 0b + 1a 1b/" 1b1a0b0a

    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.
      I was making a general point. With the sort function as you give it, there's still a difference, as shown by:
      $ perl -wle'use overload q!""!=>sub{0},fallback=>1; $x=bless[0]; $y=bl +ess[1]; print map @$_, sort { $b <=> $a or $b cmp $a } $x, $y; print map @$_, reverse sort { $a <=> $b or $a cmp $b } $x, $y' 01 10

        True, but then there are no guarentees that sort will produce consistant result if you do weird things with overload:

        perl> use overload '""' => sub{ rand }, fallback => 1;; perl> @n = map{ bless \$_ } 1 .. 10;; perl> print $$_ for sort{ $a <=> $b } @n;; 10 9 8 4 5 3 7 6 2 1 perl> print $$_ for sort{ $b <=> $a } @n;; 10 5 4 6 3 9 7 8 1 2

        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.
Re^2: sort direction
by Perl Mouse (Chaplain) on Oct 10, 2005 at 09:11 UTC
    Considering that the OP is sorting hash keys, stability is not relevant.
    Perl --((8:>*