Nye*_*ard 9 string groovy compare
嘿我已经创建了一个Groovy脚本,它将提取某些文件夹的版本号.然后我想比较版本号并选择最高版本.
我让我的脚本通过dir文件夹运行,然后我得到这种格式的版本: 02.2.02.01
所以我可以得到这样的东西:
我没有它们作为列表,但像这样:
baseDir.listFiles().each { file ->
def string = file.getName().substring(5, 15)
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
此外,我已经测试过Groovy可以将它们与>运营商进行比较,它可以!但现在我需要选择版本最高的那个
tim*_*tes 11
如果我们要求最短的答案,这必须接近;-)
String mostRecentVersion( List versions ) {
versions.sort( false ) { a, b ->
[a,b]*.tokenize('.')*.collect { it as int }.with { u, v ->
[u,v].transpose().findResult{ x,y-> x<=>y ?: null } ?: u.size() <=> v.size()
}
}[-1]
}
Run Code Online (Sandbox Code Playgroud)
这似乎有效
String mostRecentVersion(List versions) {
def sorted = versions.sort(false) { a, b ->
List verA = a.tokenize('.')
List verB = b.tokenize('.')
def commonIndices = Math.min(verA.size(), verB.size())
for (int i = 0; i < commonIndices; ++i) {
def numA = verA[i].toInteger()
def numB = verB[i].toInteger()
println "comparing $numA and $numB"
if (numA != numB) {
return numA <=> numB
}
}
// If we got this far then all the common indices are identical, so whichever version is longer must be more recent
verA.size() <=> verB.size()
}
println "sorted versions: $sorted"
sorted[-1]
}
Run Code Online (Sandbox Code Playgroud)
这是一组不充分的测试.你应该再添加一些.
assert mostRecentVersion(['02.2.02.01', '02.2.02.02', '02.2.03.01']) == '02.2.03.01'
assert mostRecentVersion(['4', '2']) == '4'
assert mostRecentVersion(['4.1', '4']) == '4.1'
assert mostRecentVersion(['4.1', '5']) == '5'
Run Code Online (Sandbox Code Playgroud)
在Groovy控制台中运行此代码和测试以验证它是否有效
我的是最短的!哈哈 )
versions = versions.sort {a, b ->
def a1 = a.tokenize('.')*.toInteger(), b1 = b.tokenize('.')*.toInteger()
for (i in 0..<[a1.size(), b1.size()].min())
if (a1[i] != b1[i]) return a1[i] <=> b1[i]
0
}
Run Code Online (Sandbox Code Playgroud)
如果有人在使用 Grails(例如 Grails 2.2.3),我认为 VersionComparator 已经提供了我们所需要的。
如果你不使用 Grails,你可以随时谷歌这个类的源代码。
工作测试示例:
import org.codehaus.groovy.grails.plugins.VersionComparator
assert ['1.13.4', '1.4.5'].sort( new VersionComparator() ) == ['1.4.5', '1.13.4']
assert ['3.1.20', '3', '3.0.1', '3.1'].sort( new VersionComparator() ) == ['3', '3.0.1', '3.1', '3.1.20']
assert ['02.2.02.02', '02.2.03.01', '02.2.02.01'].sort( new VersionComparator() ) == ['02.2.02.01', '02.2.02.02', '02.2.03.01']
assert ['4', '2'].sort( new VersionComparator() ) == ['2', '4']
assert ['4.1', '4'].sort( new VersionComparator() ) == ['4', '4.1']
assert ['4.1', '5'].sort( new VersionComparator() ) == ['4.1', '5']
assert new VersionComparator().compare( '1.13.4', '1.4.5' ) > 0
assert new VersionComparator().compare( '1.4.5', '1.13.4' ) < 0
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
小智 5
String maxVersion(versions) {
versions.max { a, b ->
List verA = a.tokenize('.')
List verB = b.tokenize('.')
def commonIndices = Math.min(verA.size(), verB.size())
for (int i = 0; i < commonIndices; ++i) {
def numA = verA[i].toInteger()
def numB = verB[i].toInteger()
if (numA != numB) {
return numA <=> numB
}
}
verA.size() <=> verB.size()
}
}
Run Code Online (Sandbox Code Playgroud)