in reply to Re^2: sort direction
in thread sort direction

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

Replies are listed 'Best First'.
Re^4: sort direction
by BrowserUk (Patriarch) on Oct 10, 2005 at 04:36 UTC

    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.

        Sorry ysth. What you were demonstrating with your use of overload went right over my head (a mention of sort stability would have been a cluebat).


        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.