Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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

by tye (Sage)
on Mar 25, 2005 at 05:09 UTC ( #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
Node Status?
node history
Node Type: note [id://442285]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2021-05-06 19:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (79 votes). Check out past polls.

    Notices?