Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Array Sort

by anonymized user 468275 (Curate)
on May 10, 2011 at 10:12 UTC ( [id://903945]=note: print w/replies, xml ) Need Help??


in reply to Array Sort

An alternative optimising solution to the Schwartzian transform suggested by grandfather is the "Orcish Manoeuvre", which also ensures that the operation evaluating the sort key is only applied once per entry, while using the same hash overhead, but often slightly less processing:
my @sorted; { local %_ = (); @sorted = sort { $_{ $a } ||= substr( $a, 3, 4 ) cmp $_{ $b } ||= substr( $b, 3, 4 ) } @array; }
BUT, in most cases, I tend to simply pass through the data once creating the inverse hash to that:
my %hash = map { substr( $_, 3, 4 ), $_ } @array;
Such a hash, using the key info as a hash key, tends to be more useful than a sorted array of keys in the subsequent processing.

Note: if the middle field should vary in length, then the expression ( split /\-/ )[1] should replace the substr function.

One world, one people

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-24 12:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found