in reply to Sorting sorts 13 as less than 2

Use spaceship (<=>) instead of cmp:
my @result = sort { $one{$b} <=> $one{$a} or $b <=> $a } keys %one;

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re^2: Sorting sorts 13 as less than 2
by pKai (Priest) on Mar 22, 2006 at 20:55 UTC

    Though the title is quite leading, the OP didn't tell us much about values %one.

    Without knowing about that side, we can't tell if that value comparision should be part of the sort function at all or if a switch in that part from cmp to <=> is advisable...

    H:\perl>perl my %one = ( 4 => 1, 3 => 2, 2 => 3, 14 => 4, 13 => 5, ); my @result = reverse sort { $one{$b} <=> $one{$a} or $b <=> $a } keys +%one; print join $/ => @result; ^Z 4 3 2 14 13

    ;-)