Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Sorting on multiple variables

by ikegami (Patriarch)
on Jul 22, 2009 at 23:45 UTC ( [id://782496]=note: print w/replies, xml ) Need Help??


in reply to Sorting on multiple variables

sort { my ($a1,$a2,$a3) = $a =~ /(\d*)_FFF(\d*)_(\d*)/; my ($b1,$b2,$b3) = $b =~ /(\d*)_FFF(\d*)_(\d*)/; $a1 <=> $b1 || $a2 <=> $b2 || $a3 <=> $b3 }

Update: If you want to avoid the redundancy:

map $_->[0], sort { $a->[1] <=> $b->[1] || $a->[2] <=> $b->[2] || $a->[3] <=> $b->[3] } map [ $_, /(\d*)_FFF(\d*)_(\d*)/ ],

Replies are listed 'Best First'.
Re^2: Sorting on multiple variables
by hobbs (Monk) on Jul 23, 2009 at 09:59 UTC
    Proof that if your sorting key is complex enough, the Schwartzian Transform simplifies a sort rather than being merely an optimization; it saves you from repeating yourself or introducing temporaries into a sort block. :)
Re^2: Sorting on multiple variables
by cbdoc (Novice) on Jul 23, 2009 at 16:51 UTC
    Amazing! That worked like a charm. Thanks very much!!

Log In?
Username:
Password:

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

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

    No recent polls found