Reputation: 61
I am trying to get the matched version of B='1.7' and start iteration and deploying the version till it match to version A='1.12' but it's not doing as expected.
def C = ['1.0', '1.6', '1.7', '1.7.1', '1.10.0', '1.11.0', '1.12']
A='1.12'
B='1.7'
for(item in C){
//println item
if (B >= item && A <= item) {
println "deploy the version ${item}"
}
}
Output-
deploy the version 1.7
deploy the version 1.12
Expected output
deploy the version 1.7
deploy the version 1.7.1
deploy the version 1.10.0
deploy the version 1.11.0
deploy the version 1.12
Please help me what I am doing wrong and just want to know is this a correct way of version comparison?
Upvotes: 0
Views: 195
Reputation: 845
I am assuming that your input list may not always be sorted. In that case:
def c = ['1.10.1', '1.0', '1.6', '1.7', '1.7.1', '1.10.0', '1.11.0', '1.12']
def high='1.12'
def low='1.7'
def compareVals = { leftVal, rightVal ->
[leftVal, rightVal]*.tokenize('.')
.with { a, b -> [a, b].transpose()
.findResult { x, y -> (x.toInteger() <=> y.toInteger()) ?: null}
?: a.size() <=> b.size()
}
}
c.sort{s1, s2 -> compareVals(s1,s2)}
.subList(c.indexOf(low), c.indexOf(high)+1)
.each {println "Deploy Version ${it}"}
OUTPUT:
Deploy Version 1.7
Deploy Version 1.7.1
Deploy Version 1.10.0
Deploy Version 1.10.1
Deploy Version 1.11.0
Deploy Version 1.12
Upvotes: 1