in reply to Re: sort direction
in thread sort direction

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.

Replies are listed 'Best First'.
Re^3: sort direction
by ysth (Canon) on Oct 10, 2005 at 04:24 UTC
    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.
        I'm not sure what your point is; mine was that sort is expected to be stable, which means that reversing $a and $b and reversing the return of sort will produce opposite orders for elements that compare as equal by the compare function. I used overload to demonstrate that this holds even when the two elements have the same string and numeric value. Making the elements take varying numeric values doesn't seem to me to be at all related.