Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the version sort order reversed?

Tags:

sorting

perl

I have the following code to sort version numbers:

chomp(my @versions = <>);  
my @sorted_versions = sort {  
                        version->parse( $a ) cmp version->parse( $b )
                    } @versions;  
print "$_\n" for @sorted_versions;  

If I pipe the following file:

cat version_file   
15.8  
15.8.1  

The output is:

15.8.1  
15.8  

Why? It should have been:

15.8  
15.8.1  

I have checked that the cmp returns 1 when running the script

like image 571
Jim Avatar asked Mar 02 '26 00:03

Jim


1 Answers

The documentation for version has this

Dotted-decimal: bare v-strings (v1.2.3) and strings with more than one decimal point and a leading 'v' ("v1.2.3"); NOTE you can technically use a v-string or strings with a leading-v and only one decimal point (v1.2 or "v1.2"), but you will confuse both yourself and others

The problem is that 15.8 parses as v5.800.0 whereas 15.8.1 becomes v15.8.1

You can read more of relevance at version::Internals

like image 61
Borodin Avatar answered Mar 04 '26 14:03

Borodin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!