blazar's solution works properly, unlike some of the other solutions given above (++ for him), but mine is more efficient: 2.08 vs 3.68 seconds for 1000 iterations with 100 randomly generated items. Smaller sets show much the same ratio.
use strict;
use warnings;
### Randomly generate numbers
my @array;
for (0..100) {
my @n;
push @n, (int rand 15) + 1;
for (0..((int rand 3) - 1)) {
push @n, int rand 15;
}
push @array, join '-', @n;
}
### Sort the numbers
my @arr = map { $_ = join '-', @$_ }
sort { mysort($a, $b) }
map { $_ = [split '-'] } @array;
print join "\n", @arr;
### mysort does the actual comparisons
sub mysort {
my ($s1, $s2) = ($#{$_[0]}, $#{$_[1]});
for (0..($s1 < $s2 ? $s2 : $s1)) {
no warnings;
return $_ if $_ = $_[0][$_] <=> $_[1][$_];
}
### N comes before N-0
return ($s1 < $s2 ? -1 : ($s2 > $s1 ? 1 : 0));
}