print for sort qw[ 12 a123 a122 A123 B123 Ab123 aB123 456 1A23 1a23 ];;
12 ## ord('1') == 49, ord('2') == 50
1A23 ## ord('A') == 65 so this comes after.
1a23 ## ord('a') == 97 "
456 ## ord('4') == 52 "
A123 ## ord('A') == 65, ord('1') == 49 "
Ab123 ## ord('b') == 98 "
B123 ## etc.
a122
a123
aB123
####
print for sort{ $b cmp $a } qw[ 1 10 100 2 20 21 3 300 ];;
300
3
21
20
2
100
10
1
##
##
print for sort{ $b <=> $a } qw[ 1 10 100 2 20 21 3 300 ];
300
100
21
20
10
3
2
1
##
##
print for sort{
substr( $a, 1 ) <=> substr( $b, 1 )
} qw[ A473 B659 C123 D222 E001 ];;
E001
C123
D222
A473
B659
##
##
## Build an array of anonymous arrays,
## each of which contains the sort field and the original element.
@anons = map{ [ substr( $_, 1 ) , $_ ] } qw[ A473 B659 C123 D222 E001 ];;
print Dumper \@anons;;
$VAR1 = [
['473','A473'],['659','B659'],['123','C123'],
['222','D222'],['001','E001']
];
## Now sort the anonymous arrays
## by comparing the extracted fields.
@sortedAnons = sort{ $a->[ 0 ] <=> $b->[ 0 ] } @anons;;
print Dumper \@sortedAnons;;
$VAR1 = [
['001','E001'],[123,'C123'],[222,'D222'],
[473,'A473'],[659,'B659']
];
## Finally, build the required sorted array
## by extracting the original elements discarding the sort fields.
@sorted = map{ $_->[ 1 ] } @sortedAnons;;
print Dumper \@sorted;;
$VAR1 = ['E001','C123','D222','A473','B659'];
##
##
@sorted = map{
$_->[1]
} sort{
$a->[0] <=> $b->[0]
} map{
[ substr( $_, 1 ), $_ ]
} qw[ A473 B659 C123 D222 E001 ];;
print Dumper \@sorted;;
$VAR1 = ['E001','C123','D222','A473','B659'];
##
##
print for map{
$_->[2]
} sort{
$a->[0] <=> $b->[0]
||
$a->[1] cmp $b->[1]
} map{
[ substr( $_, 1 ), substr( $_, 0, 1 ), $_ ]
} qw[ A473 B437 B659 C659 C123 D123 D222 E222 E001 A001 ];;
A001
E001
C123
D123
D222
E222
B437
A473
B659
C659
##
##
print for map{
## Chop off the bit we added.
substr( $_, 3 )
} sort map{ ## Note: No comparison block callback.
## Extract the field as before, but concatenate it with the original element
## instead of building an anonymous array containing both elements.
substr( $_, 1 ) . $_
} qw[ A473 B659 C123 D222 E001 ];;
E001
C123
D222
A473
B659
##
##
print for map{
unpack 'x[N] A*', $_
} sort map{
pack 'N A*', substr( $_, 1 ), $_
} qw[ A473 B659 C123 D222 E001 ];;
E001
C123
D222
A473
B659
##
##
print for map{
unpack 'x[NA1]A*', $_
} sort map{
pack 'NA1 A*', substr( $_, 1 ), substr( $_, 0, 1 ), $_
} qw[ A473 B437 B659 C659 C123 D123 D222 E222 E001 A001 ];
A001
E001
C123
D123
D222
E222
B437
A473
B659
C659
##
##
#! Perl -slw
use strict;
my %months = (
FY => 0,
Jan => 1, Feb => 2, Mar => 3, Apr => 4, May => 5, Jun => 6,
Jul => 7, Aug => 8, Sep => 9, Oct => 10, Nov => 11, Dec => 12,
);
print for map{
unpack 'x[nn]A*', $_
} sort map {
my( $alpha, $num ) = m[^(\S+?)\s*(\d+)$];
$num += 2000 if $num <= 49;
$num += 1900 if $num <= 99;
pack 'nnA*', $num, $months{ $alpha }, $_;
} ;
__DATA__
Apr 2006
FY05
FY98
FY04
Dec 2007
Jan 1997
Jan 1998
Dec 1998
##
##
C:\test>junk
Jan 1997
FY98
Jan 1998
Dec 1998
FY04
FY05
Apr 2006
Dec 2007