Created
May 28, 2017 23:21
-
-
Save tateisu/f44fee069f2f9c660a7b50a95adc8a20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!perl -- | |
## usage: git tag |perl sortVersionString.pl | |
use warnings; | |
sub tokenize{ | |
my @r; | |
while( $_[0] =~ /(?:\.?)(\d+|\D+)/g ){ | |
push @r,$1; | |
} | |
return @r; | |
} | |
sub compareVersionString{ | |
my @a = tokenize( $_[0] ); | |
my @b = tokenize( $_[1] ); | |
for(;;){ | |
my $a = shift @a; | |
my $b = shift @b; | |
if( not defined($a) ){ | |
return defined($b) ? -1 : 0; | |
}elsif( not defined($b) ){ | |
return 1; # $a is defined | |
} | |
next if( $a eq $b ); | |
if( $a =~ /\d+/ ){ | |
if( $b =~ /\d+/ ){ | |
my $i = $a - $b; | |
$i and return $i<0? -1 : 1; | |
}else{ | |
return 1; # $a=1.4.1, $b=1.4rc, ".1" is greater than "rc". | |
} | |
}elsif( $b =~ /\d+/ ){ | |
# $a is not digits. | |
return -1; # $a=1.4rc, $b=1.4.1, "rc" is lesser than ".1". | |
} | |
return $a cmp $b; | |
} | |
} | |
my @a; | |
while(<>){ | |
s/[\x0d\x0a]+//; | |
s/^\s+//; | |
s/\s+$//; | |
length and push @a,$_; | |
} | |
@a = sort { compareVersionString($a,$b) } @a; | |
for(@a){ | |
print "$_\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment