in reply to sort by a multiple columns
The mess on the for line determines how many elements there are in the longer of the two arrays being compared. The comparison is by number, then by string (so that null sorts before 0). If there's no difference either way, we continue through the loop. If there is a difference, it returns.print join "\n", map { join ',', @$_ } sort { for (0..($#{$a}<$#{$b}?$#{$b}:$#{$a})) { return $a->[$_] <=> $b->[$_] || $a->[$_] cmp $b->[$_] || next } } @un_sorted;
Update: I came up with a better way of handling different-length arrays below.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: sort by a multiple columns
by BrowserUk (Patriarch) on Sep 08, 2004 at 19:27 UTC | |
by CombatSquirrel (Hermit) on Sep 08, 2004 at 21:58 UTC | |
by Roy Johnson (Monsignor) on Sep 08, 2004 at 23:20 UTC | |
by BrowserUk (Patriarch) on Sep 08, 2004 at 22:20 UTC | |
by Roy Johnson (Monsignor) on Sep 08, 2004 at 19:36 UTC |