Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Alpha number sort (one, true, natural sort)

by tye (Sage)
on Mar 25, 2005 at 05:09 UTC ( [id://442285]=note: print w/replies, xml ) Need Help??


in reply to Alpha number sort

A simple search here for natural sort turns up lots of techniques. Here is a rewrite of my favorite using the "one true sort" (fast, flexible, stable sort).

#!/usr/bin/perl -w use strict; my @list= <DATA>; my @sorted= @list[ map { unpack "N", substr($_,-4) } sort map { my $key= $list[$_]; $key =~ s[(\d+)][ pack "N", $1 ]ge; $key . pack "N", $_ } 0..$#list ]; print @sorted; __END__ a1.5 a1.5b a1.55 a1.55b a1.6 a1.6b linux-2.4.28.tar linux-2.4.29.tar linux-2.4.29a.tar linux-2.4.3.tar linux-2.10.6.tar linux-2.10.50.tar

Producing

a1.5 a1.5b a1.6 a1.6b a1.55 a1.55b linux-2.4.3.tar linux-2.4.28.tar linux-2.4.29.tar linux-2.4.29a.tar linux-2.10.6.tar linux-2.10.50.tar

- tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://442285]
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: (6)
As of 2024-04-23 13:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found