Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Keep It Simple, Stupid
 
PerlMonks  

Re: How do I do a natural sort on an array?

by tye (Sage)
on Mar 29, 2001 at 20:15 UTC ( [id://68205]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to How do I do a natural sort on an array?

my @sorted= grep {s/(^|\D)0+(\d)/$1$2/g,1} sort grep {s/(\d+)/sprintf"%06.6d",$1/ge,1} @data;
See RE: RE: Re: Sorting on Section Numbers some important style comments.

Features and limitations:

  • Strips extra leading zeros from digit strings
  • Doesn't handle floating point numbers at all well
  • Sorts negative integers in reverse after positive integers
  • Uses very little extra memory
  • You must specify the maximum number of digits your integers will have (6 in the above code)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://68205]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.