Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Alpha number sort

by Crackers2 (Parson)
on Mar 25, 2005 at 15:51 UTC ( #442360=note: print w/replies, xml ) Need Help??


in reply to Alpha number sort

I seems to me the preferred sort order in your example is different from what you describe in the text, and both text and example appear somewhat inconsistent.

For example, you say you want linux releases in release order, yet you put linux-2.4.3 after linux-2.4.28. Same thing with 2.10.6 vs 2.10.50

The core inconsistency seems to be this part of your request: "x.10 should come after x.9, but x.55 should come before x.6".

Perhaps (since this seems to be about comparing release versions of software packages) you need to find a way to use a different compare function for different packages, and the n either use a lookup table or a heuristic to figure out which one to use.

Replies are listed 'Best First'.
Re^2: Alpha number sort
by gam3 (Curate) on Mar 25, 2005 at 23:47 UTC
    Thank you for pointing out the problem in the data section.

    As for the inconsistency, there is not one because I am saying that \d+\.\d+ is a floating point number but that \d+\.\d+\. is not.

    I agree that this will cause some version numbering schemes not to work, or for floating point numbers not to work if followed by a decimal number. But then if it was easy I would not need to post to perlmonks would I.

    -- gam3
    A picture is worth a thousand words, but takes 200K.

      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        

        It is always nice to see code that is smaller and faster. Two times faster an 6 times smaller.
              Rate gam3  tye
        gam3 243/s   -- -69%
        tye  772/s 218%   --
        
        The regex still had one bug: you need a \d in the backtrace to keep it from matching 2.11. on 2.1/1.
        qr/((?<!\.)(\d+)\.\d+(?![\.\d])|\d+)/
        I would never have thought of using s///g in place of my while loop.
        -- gam3
        A picture is worth a thousand words, but takes 200K.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2021-08-02 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)








    Results (29 votes). Check out past polls.

    Notices?