In Groovy - 如何在确切的索引位置迭代版本列表并在for循环中进行比较?
In Groovy -How to iterate the version list at the exact index position and compare it in for loop?
我正在尝试获取 B='1.7' 的匹配版本并开始迭代并部署该版本,直到它与 A='1.12' 版本匹配,但它没有按预期运行。
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}"
}
}
输出-
deploy the version 1.7
deploy the version 1.12
预期输出
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
请帮我看看我做错了什么,只是想知道这是版本比较的正确方法吗?
我假设您的输入列表可能并不总是排序。在那种情况下:
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}"}
输出:
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
我正在尝试获取 B='1.7' 的匹配版本并开始迭代并部署该版本,直到它与 A='1.12' 版本匹配,但它没有按预期运行。
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}"
}
}
输出-
deploy the version 1.7
deploy the version 1.12
预期输出
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
请帮我看看我做错了什么,只是想知道这是版本比较的正确方法吗?
我假设您的输入列表可能并不总是排序。在那种情况下:
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}"}
输出:
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