If values are non-repeated, as it appears in %order, you could also use a reversed version of the hash to do the sort. Please note that this doesn't hold if there are repeated values in %order!
In that less general hypothesis, you could do:
my @ordered ; my %r_order = reverse %order ; # %r_order is now ( 1 => 'DATE', 2=> 'CPP'... # ...and so on. { # slice %order in @temp # (the following two lines could be compacted into one # but... KISS :-) my @temp = sort { $a <=> $b } @order{@to_be_ordered} ; @ordered = @r_order{@temp} ; # @temp will now disappear :-) } # Now @ordered contains the sorted list based on # @to_be_ordered, so... &do_what_you_want_with(@ordered) ;
That should work.
Ciao!
--bronto
In reply to Re: Sorting an array based on hash values
by bronto
in thread Sorting an array based on hash values
by kevsurf
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |