in reply to Sorting large sets of geometric coordinates
The spaceship (<=>) returns 0 when the two items being compared are equal so use an or to fall back to a secondary comparison if the first compare finds a match.
use strict; use warnings; my @lines = <DATA>; @lines = map {[/(\d+\.\d+)/g]} @lines; @lines = sort {$a->[1] <=> $b->[1] or $a->[0] <=> $b->[0]} @lines; print "@$_\n" for @lines; __DATA__ 48.1 0.4 99.0 0.5 1.0 0.2 99.0 0.3 1.0 0.1 99.0 0.2 1.0 0.3 99.0 0.4 1.0 0.5 99.0 0.6 13.1 0.6 99.0 0.7 5.0 0.4 48.0 0.5 1.0 0.6 13.0 0.7
Prints:
1.0 0.1 99.0 0.2 1.0 0.2 99.0 0.3 1.0 0.3 99.0 0.4 5.0 0.4 48.0 0.5 48.1 0.4 99.0 0.5 1.0 0.5 99.0 0.6 1.0 0.6 13.0 0.7 13.1 0.6 99.0 0.7
|
|---|