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 anons ## by comparing the extracted fields from within the anonymous arrays. @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'; $VAR2 = 'C123'; $VAR3 = 'D222'; $VAR4 = 'A473'; $VAR5 = '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