split /(\d+)/
####
sub natcomp {
my @a = split /(\d+)/, $a;
my @b = split /(\d+)/, $b;
my $last = min(scalar @a, scalar @b)-1;
my $cmp;
for my $i (0 .. $last) {
unless($i & 1) { # even
$cmp = lc $a[$i] cmp lc $b[$i] || $a[$i] cmp $b[$i] and return $cmp;
}else { # odd
$cmp = $a[$i] <=> $b[$i] and return $cmp;
}
}
return scalar @a <=> scalar @b; # shortest array comes first
}
####
chomp(my @array = );
$\ = "\n";
print for sort natcomp @array;
__DATA__
1
A
amstelveen
Amsterdam
Amsterdam40
Amsterdam40b
Amsterdam5
Amsterdamned
Chapter 1 Section 10
Chapter 1 Section 3
Chapter 10 Section 2
Chapter 2 Section 1