in reply to Sorting multi-dimensional arrays
If your set of prefixes if reasonably small, but the format and/or the variations in format make it difficult to map the sort order cleanly, then another possibility is to use a lookup table for your sorting. That way you pre-specify the order you want them in and let sort use that pre-specified order to do its work.
This isn't a very convincing demonstration, but it should serve as an illustration. It's greatest asset is its flexibility, though on large datasets it should also be pretty efficient.
#! Perl -sw use strict; my $index = 0; my %lookup = map { $_ => $index++; } qw( CCI001 CCI002 CCI002A CCI002B CCI100 CCI101 CCI500 CDI001 CDJ001 ); my @data = ( [ qw(CDJ001 1 N)], [ qw(CCI100 1 M)], [ qw(CCI001 1 M)], [ qw(CCI002B 1 N)], [ qw(CCI002 1 N)], [ qw(CDI001 1 M)], [ qw(CCI500 1 M)], [ qw(CCI002A 1 N)], [ qw(CCI101 1 N)], ); my @sorted = sort{ $lookup{$$a[0]} <=> $lookup{$$b[0]} } @data; print "@{$_}\n" for @sorted; __DATA__ # Output C:\test>196991 CCI001 1 M CCI002 1 N CCI002A 1 N CCI002B 1 N CCI100 1 M CCI101 1 N CCI500 1 M CDI001 1 M CDJ001 1 N C:\test>
|
|---|