A bit over-engineered in this case. Take the same assumption as yours, the code could be much simpler:
my @new = sort {(split /\s+/, $b)[0] <=> (split /\s+/, $a)[0]} @old;
And it is faster:
use Data::Dumper; use strict; use warnings; my @old = ("10.5 AA", "10.6 AA", "9 AC", "2 BB"); my $t0 = time(); for (1..200000) {#yours my @new = map { $_->[0] } sort { $b->[1] <=> $a->[1] } map { [ $_, (split( /\s+/, $_, 2 ))[0] ] } @old; } print time() - $t0, "\n"; $t0 = time(); for (1..200000) {#mine my @new = sort {(split /\s+/, $b)[0] <=> (split /\s+/, $a)[0]} @ol +d; } print time() - $t0, "\n";
I ran four times, yours took: 13, 14, 14, 13 seconds, when mine took 8, 10, 11, 9 seconds.
In reply to Re^2: Numeric Sort for Stringified Value (How to Avoid Warning) (Use the ST)
by pg
in thread Numeric Sort for Stringified Value (How to Avoid Warning)
by neversaint
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |