Forked from founddrama/VersionComparator.groovy
Last active
September 16, 2022 13:59
-
-
Save keithel/ba502648369e4e594f325dffc75e329c to your computer and use it in GitHub Desktop.
a version comparator (e.g., "1.0.2" < "1.0.10")
This file contains hidden or 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
#!/usr/bin/env ruby | |
args = $*.to_a | |
if args.index("-c") | |
ct = [args[args.index("-c") + 1].to_i, 1].max | |
end | |
tags = $stdin.to_a | |
tags.sort! {|a,b| | |
a = a.split(/[\._]/) | |
b = b.split(/[\._]/) | |
def is_i(str) | |
return str =~ /^\d+$/ | |
end | |
c = 0 | |
for i in 0..([a.size, b.size].max) | |
if i == a.size | |
c = is_i(b[i]) ? -1 : 1 | |
elsif i == b.size | |
c = is_i(a[i]) ? 1 : -1 | |
end | |
if is_i(a[i]) && is_i(b[i]) | |
c = a[i].to_i <=> b[i].to_i | |
elsif is_i(a[i]) | |
c = 1 | |
elsif is_i(b[i]) | |
c = -1 | |
elsif !a[i].nil? && !b[i].nil? | |
c = a[i] <=> b[i] | |
end | |
break if c != 0 | |
end | |
c | |
} | |
if ct | |
ct = [ct, tags.size].min | |
tag_range = tags[-ct..-1] | |
puts "LAST #{ct} VERSIONS:" | |
tag_range.each {|i| puts "#{i}" } | |
else | |
puts "LATEST VERSION: #{tags[-1]}" | |
end |
This file contains hidden or 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
def versions = [] | |
def f = new File('mock-version-tags.txt') | |
f.eachLine { versions << it } | |
def versionComparator = { a, b -> | |
def VALID_TOKENS = /._/ | |
a = a.tokenize(VALID_TOKENS) | |
b = b.tokenize(VALID_TOKENS) | |
for (i in 0..<Math.max(a.size(), b.size())) { | |
if (i == a.size()) { | |
return b[i].isInteger() ? -1 : 1 | |
} else if (i == b.size()) { | |
return a[i].isInteger() ? 1 : -1 | |
} | |
if (a[i].isInteger() && b[i].isInteger()) { | |
int c = (a[i] as int) <=> (b[i] as int) | |
if (c != 0) { | |
return c | |
} | |
} else if (a[i].isInteger()) { | |
return 1 | |
} else if (b[i].isInteger()) { | |
return -1 | |
} else { | |
int c = a[i] <=> b[i] | |
if (c != 0) { | |
return c | |
} | |
} | |
} | |
return 0 | |
} | |
versions.sort(versionComparator) |
This file contains hidden or 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
// for the Node.js crew: | |
function versionComparator(a, b){ | |
a = a.split(/[._]/); | |
b = b.split(/[._]/); | |
var mx = Math.max(a.length, b.length), | |
isInt(n){ | |
return (n || '').match(/^\d+$/) !== null; | |
}, | |
c = 0, | |
ai, bi; | |
for (var i = 0; i < mx; i++) { | |
ai = a[i], | |
bi = b[i]; | |
if(c !== 0) break; | |
switch(i){ | |
case a.length: | |
return isInt(ai) ? -1 : 1; | |
case b.length: | |
return isInt(bi) ? 1 : -1; | |
default: | |
var aiInt = isInt(ai), | |
biInt = isInt(bi); | |
if (aiInt && biInt) { | |
ai = parseInt(ai); | |
bi = parseInt(bi); | |
c = ai === bi ? 0 : ai > bi ? 1 : -1; | |
if (c !== 0) break; | |
} else if (aiInt) { | |
return 1; | |
} else if (biInt) { | |
return -1; | |
} else { | |
c = ai === bi ? 0 : ai > bi ? 1 : -1; | |
break; | |
} | |
} | |
} | |
return c; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://groovyconsole.appspot.com/script/4863416825020416
^^ Interactive groovy console with version sort snippet