in reply to Sorting array
That's a potentially nasty desired sort order. It seems like it could consist of the following components:
That's a mess. But once you break the input into its individual components the sorting becomes pretty simple.
my @data = qw/ ID12-ABC-5.1 ID9-ABC-5.1 ID3-ABC-6.1 ABC-5.1 ID12-ABC-5.1.5 ID15-ABC-6.1 ABC-6.1 ID5-ABC-5.1 ID5-ABC-5.1.5 ABC-5.1.5 /; my @sorted = do{ no warnings 'uninitialized'; map { $_->[-1] } sort{ $b->[0] cmp $a->[0] or # 'ID' component. $b->[3] <=> $a->[3] or # v-string part1. $b->[4] <=> $a->[4] or # v-string part2. $b->[5] <=> $a->[5] or # v-string part3. $b->[1] <=> $a->[1] or # Numeric following 'ID'. $b->[2] cmp $a->[2] # 'ABC' component. } map { [ m/^ (?:(\p{Alphabetic}+)(\d+)-)? # "ID", then digits. (\p{Alphabetic}+)- # "ABC" component. (\d+) # v-string part1. (?:\.(\d+))? # v-string part2. (?:\.?(\d+))? # v-string part3. $/x, $_ ] } @data; }; say for @sorted;
And the output...
ID15-ABC-6.1 ID3-ABC-6.1 ID12-ABC-5.1.5 ID5-ABC-5.1.5 ID12-ABC-5.1 ID9-ABC-5.1 ID5-ABC-5.1 ABC-6.1 ABC-5.1.5 ABC-5.1
...which I think matches your specification.
To me this is a case where the Schwartzian Transform actually makes the code easier to follow. Imagine trying to do all the matching inline, within the sort routine. The biggest problem was trying to figure out how to format the code. lol
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sorting array
by fdegir (Novice) on Sep 23, 2011 at 08:55 UTC |