my @names =
map $_->[0],
sort { $b->[1] <=> $a->[1] }
map [ $_, ask_monkey_about($_) ],
@castaways;
Because the map and sort
operators are right to left, you have to read this construct from the
bottom up. Take a list of @castaways, create some
arrayrefs by asking the monkey a simple question, sort the list of
arrayrefs, and then extract the names from each arrayref. This gives
you the list of names in the desired order.
This construct is commonly called the Schwartzian
Transform, which was named after me (but not by me),
thanks to a Usenet posting I made many years ago. It has proven to be
a very nice thing to have in your bag of sorting tricks.
If this transform looks like it might be too complex to memorize or
come up with from first principles, it might help to look at the
flexible and constant parts:
my @output_data =
map $_->[0],
sort { SORT COMPARISON USING $a->[1] AND $b->[1] }
map [ $_, EXPENSIVE FUNCTION OF $_ ],
@input_data;