in reply to Reverse sort array
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) )
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Reverse sort array
by rethaew (Sexton) on Aug 19, 2009 at 22:36 UTC |