my @sorted =
map { $_->[0] }
sort { $a->[1] cmp $b->[1] } # or <=>, or $b before $a
map { [$_, f($_)] }
@unsorted;
####
# the first map does this
qw(8 11 9 4 3) --> (
[ 8, "viii"],
[11, "xi"],
[ 9, "ix"],
[ 4, "vi"],
[ 3, "iii"],
)
##
##
# the sort does this:
(
[ 8, "viii"],
[11, "xi"],
[ 9, "ix"],
[ 4, "vi"],
[ 3, "iii"],
) --> (
[ 3, "iii"],
[ 9, "ix"],
[ 4, "vi"],
[ 8, "viii"],
[11, "xi"],
)
##
##
(
[ 3, "iii"],
[ 9, "ix"],
[ 4, "vi"],
[ 8, "viii"],
[11, "xi"],
) --> qw( 3 9 4 8 11 )
##
##
use strict;
use warnings;
use Roman ();
my @unsorted = qw(8 11 9 4 3);
*f = \&Roman::roman;
my @sorted =
map { $_->[0] }
sort { $a->[1] cmp $b->[1] } # or <=>, or $b before $a
map { [$_, f($_)] }
@unsorted;
print "@sorted\n";
__END__ # output shown below
3 4 9 8 11
##
##
my @sorted = sort { f($a) cmp f($b) } @unsorted;
##
##
*f = sub {
$::count++;
END { print "f() called $::count times\n" };
goto \&Roman::roman;
};
##
##
use List::Util "shuffle";
my @unsorted = shuffle(1..100);