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

Version Sorting

by perl_lover (Chaplain)
on May 30, 2006 at 09:35 UTC ( #552438=perlquestion: print w/ replies, xml ) Need Help??
perl_lover has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I have an array of version numbers.
my @version = ("1.2.3","1.3.4","1.2.31","1.12","1.12.1","1.12.12");

I need to sort this version numbers. Is there any efficent way in sorting version ?
The order should be
1.2.3, 1.2.31, 1.3.4, 1.12, 1.12.1, 1.12.12

Comment on Version Sorting
Download Code
Re: Version Sorting
by Samy_rio (Vicar) on May 30, 2006 at 09:44 UTC

    Hi perl_lover, Try this using module,

    use strict; use warnings; use Sort::Versions; my @version = ("1.2.3","1.3.4","1.2.31","1.12","1.12.1","1.12.12") +; my @l = sort { versioncmp($a, $b) } @version; $,=", "; print @l,"\n"; __END__ 1.2.3, 1.2.31, 1.3.4, 1.12, 1.12.1, 1.12.12,

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: Version Sorting
by jesuashok (Curate) on May 30, 2006 at 09:46 UTC
Re: Version Sorting
by planetscape (Canon) on May 30, 2006 at 09:49 UTC
Re: Version Sorting
by TedPride (Priest) on May 30, 2006 at 10:06 UTC
    The following does what you need:
    use strict; use warnings; chomp(my @version = <DATA>); print join "\n", @version = map { join '.', unpack 'C*', $_ } sort map { pack 'C*', split /\./, $_ } @version; __DATA__ 1.2.3 1.3.4 1.2.31 1.12 1.12.1 1.12.12
    Basically, it converts each number part to a byte value, so the resulting version string is fixed-width (for the number of fields it has) and can be efficiently sorted. Then it sorts and converts back.
Re: Version Sorting
by borisz (Canon) on May 30, 2006 at 10:07 UTC
    use version; my @version = ( "1.2.3", "1.3.4", "1.2.31", "1.12", "1.12.1", "1.12.12 +" ); @version = map { $_->[1] } sort { $a->[0] cmp $b->[0] } map { [ qv($_), $_ ] } +@version; __OUTPUT__ 1.2.3, 1.2.31, 1.3.4, 1.12, 1.12.1, 1.12.12
    Boris
Re: Version Sorting
by ioannis (Priest) on May 30, 2006 at 11:14 UTC
    What about v-strings, or the other equivalent forms of version number ?

    Something similar to the version module will be part of Perl 5.10.0 . In the example bellow, note that $a could also have been '1.2.3' or other forms of version number (see perldoc).

    use version; # Version comparison # '1.2.3', 'v1.2.3', 1.2.3 my $a = qv( 'v1.2.3' ); my $b = qv( '1.2.29' ); # Compare print +($a>$b) ? $a : $b;
Reaped: Re: Version Sorting
by NodeReaper (Curate) on May 30, 2006 at 23:32 UTC
Re: Version Sorting
by gam3 (Curate) on May 30, 2006 at 23:40 UTC
    You might also want to take a look at Re^3: Alpha number sort (two truer to spec). As it is a more complex example of Re: Version Sorting. However it does not solve your problem directly as it assumes 1.12 is a floating point number rather than a version number.
    -- gam3
    A picture is worth a thousand words, but takes 200K.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://552438]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2014-07-26 09:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls