Why not just do the reverse when requested?
You could use eval STRING, but that's evil, so let's avoid that. Another possibility is:my @ordered = sort @array; @ordered = reverse @array if $something;
but I don't really like that - too much copying (especially if you have a sort function). Another one that is close to being nice is:foreach ($something ? reverse sort @array : sort @array)
(Untested - you may need to pass in \@array.) Along these lines, you could use the multikeysorter from Sort::Key) to create a sorter sub that sorts in the correct order - you pass in something based on the $something.use Sort::Key qw(keysort rkeysort); my $sorter = $something ? \&rkeysort : \&keysort; foreach ($sorter->(sub { $_ }, @array) )
In reply to Re: Reverse sort array
by Tanktalus
in thread Reverse sort array
by rethaew
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |