my @sorted = sort mysort @unsorted;
sub mysort
{
$a =~ m/(.*?)\s*(\S+)$/;
$aa = "$2 $1";
$b =~ m/(.*?)\s*(\S+)$/;
$bb = "$2 $1";
return $aa cmp $bb;
}
####
@unsorted = ("Larry Wall", "Arthur C. Clarke");
@a = map { m/(.*?)\s*(\S+)$/; [$_, "$2 $1" ] } @unsorted;
#
# now @a is (["Larry Wall", "Wall Larry"],
# ["Arthur C. Clarke", "Clarke Arthur C."])
#
##
##
my @sorted =
map{ $_->[0] }
sort {$a->[1] cmp $b->[1]}
map { m/(.*?)\s*(\S+)$/; [$_, "$2 $1" ] }
@unsorted;
##
##
#!/usr/bin/perl
use Benchmark;
@unsorted = ("Larry Wall",
"Jane Sally Doe",
"John Doe",
"Morphius",
"Jane Alice Doe",
"Arthur C. Clarke");
timethese(100000, {
"schwartzian" => sub { my @sorted =
map{ $_->[0] }
sort {$a->[1] cmp $b->[1]}
map { m/(.*?)\s*(\S+)$/; [$_, "$2 $1" ] }
@unsorted },
"sort routine" => sub { my @sorted = sort mysort @unsorted }
});
sub mysort
{
$a =~ m/(.*?)\s*(\S+)$/;
$aa = "$2 $1";
$b =~ m/(.*?)\s*(\S+)$/;
$bb = "$2 $1";
return $aa cmp $bb;
}
##
##
Benchmark: timing 100000 iterations of schwartzian, sort routine...
schwartzian: 57 wallclock secs (53.66 usr + 0.30 sys = 53.96 CPU)
sort routine: 124 wallclock secs (122.48 usr + 0.41 sys = 122.89 CPU)