in reply to Reverse sort array

sort allows for a custom compare function.
my $sorter = ( $reverse ? sub { $b cmp $a } : sub { $a cmp $b } ); for (sort $sorter @array) { ... }

Replies are listed 'Best First'.
Re^2: Reverse sort array
by rethaew (Sexton) on Aug 19, 2009 at 22:30 UTC
    Thank you for your reply. I am sorry but it is not immediately clear to me how to use this code you have written.
      my $reverse = its_reverse_sorting(time); # set or clear flag my $sorter = ( $reverse ? sub { $b cmp $a } : sub { $a cmp $b } ); for (sort $sorter @array) { ... }
      I renamed $something to $reverse, but it's otherwise a drop-in replacement for the code you posted.
Re^2: Reverse sort array
by rovf (Priest) on Aug 20, 2009 at 08:51 UTC

    Or, if you want to do it without ?: and add a slight touch of obfuscation:

    for (sort { "-"x!!$reverse.1*($a cmp $b) } @array) { ... }

    -- 
    Ronald Fischer <ynnor@mm.st>
      If I wanted to avoid the conditional operator, it would be because I had many options. In that case, I would use a lookup table.
      my $sorter = $lkup{$method}{$direction}; for (sort $sorter @array) { ... }