in reply to Optimizing a sort function (wrap-around alpha)

I get your desired effect with this:
use strict; use warnings; use List::Util 'shuffle'; my @list = shuffle('A'..'N', 'I'..'K'); my $pick = 'J'; print sort {(($a lt $pick) == ($b lt $pick)) ? ($a cmp $b) : ($b cmp $ +a)} @list;
Technically, each of those lt's should be a ternary operation, because values for true could be different.

Update: this GR transorm also works:

use strict; use warnings; use List::Util 'shuffle'; my @list = shuffle('A'..'N', 'I'..'K'); my $pick = 'J'; ++(my $after_pick = $pick); print map substr($_,1), sort map {$_ lt $pick ? "$after_pick$_" : "$pi +ck$_"} @list;

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: Optimizing a sort function (wrap-around alpha)
by tlm (Prior) on Apr 15, 2005 at 18:24 UTC

    Technically, each of those lt's should be a ternary operation, because values for true could be different.

    How about using ! xor instead of == ?

    !(($a lt $pick) xor ($b lt $pick)) ? ...

    Update: Alternatively, you could "booleanize" each side of the comparison with !:

    ( !($a lt $pick) == !($b lt $pick) ) ? ...
    Hmmm, I think ! xor is clearer; it's the "semantically correct" way to test two booleans for equality.

    the lowliest monk

Re^2: Optimizing a sort function (wrap-around alpha)
by SineSwiper (Beadle) on Apr 15, 2005 at 13:45 UTC
    Yeah, that first one works, and looks a lot better. I knew I was making it too complex. Thanks.