Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Sorting large sets of geometric coordinates

by GrandFather (Saint)
on Apr 19, 2006 at 21:01 UTC ( [id://544438]=note: print w/replies, xml ) Need Help??


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

DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://544438]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-19 15:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found