in reply to sorting an array with mixed elements...
print sort { (split ":", $a)[0] <=> (split ":", $b)[0] } @table;
sort {...} @table will sort the contents of the array @table according to the contents of {...}
sort works by comparing pairs of elements, calling them $a and $b within the {...}.
split ":", $a will split $a into a list using ":" as the separator
(...)[0] will return the first element of that list
So, given input like:
111:A:B;C 2:D:E:F
This will sort by looking at the first field only, namely "111" & "2", treating them as numbers because of the use of <=>.
update: Oops, I forgot to mention <=>, the numeric comparison operator, usually called the spaceship operator.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: sorting an array with mixed elements...
by cgmd (Beadle) on Jun 09, 2007 at 23:08 UTC | |
by lidden (Curate) on Jun 09, 2007 at 23:51 UTC | |
by FunkyMonk (Bishop) on Jun 10, 2007 at 08:18 UTC | |
by blazar (Canon) on Jun 10, 2007 at 21:38 UTC |