Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: a proper sort

by Anonymous Monk
on Mar 04, 2005 at 15:07 UTC ( #436593=note: print w/replies, xml ) Need Help??

in reply to a proper sort

This works assuming that the numbers are on the end, there are no leading 0's, and there are at most 5 digits (if more, it's easy to see where to adjust):
my @pretty = map {s/(?<=\D)0+//; $_} sort map {chomp; do {no warnings; sprintf "%s%05d", /(\D*)(\d* +)/}} @not_so_pretty;
The first map (that is, the bottom one in the code), normifies the strings, by padding 0's so all the numbers have equal length - if there are no trailing numbers, 0's will be used. Then we perform a regular sort, and finally, we strip off the leading 0's (which could mean all the 0's). This technique is one of the variations of the GRT.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (1)
As of 2023-09-24 04:48 GMT
Find Nodes?
    Voting Booth?

    No recent polls found