http://www.perlmonks.org?node_id=442435


in reply to Re^2: Alpha number sort
in thread Alpha number sort

Ah. It appears that everyone (including me) missed this.

#!/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+)\.\d+(?!\.)|\d+)][ my $len= length( defined($2) ? $2 : $1 ); pack( "N", $len ) . $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

Produces

a1.5 a1.5b a1.55 a1.55b a1.6 a1.6b 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

Update: Third-time lucky. The original regexes are below in HTML comments.

- tye