我在Groovy中有以下字典,也就是MAP。
list = [
[
name:ProductA-manifest-file.json,
path:ProductA,
properties: [
[
key:release,
value:RC1.0
],
[ key:PIPELINE_VERSION,
value:1.0.0.11
]
],
repo:some-generic-repo-local,
],
[
name:ProductA-manifest-file.json,
path:ProductA,
properties: [
[
key:release,
value:RC1.0
],
[ key:PIPELINE_VERSION,
value:1.0.0.75
]
],
repo:some-generic-repo-local,
],
[
name:ProductA-manifest-file.json,
path:ProductA,
properties: [
[
key:release,
value:RC1.0
],
[ key:PIPELINE_VERSION,
value:1.0.0.1104
]
],
repo:some-generic-repo-local,
],
[
more similar entries here containing
],
[
more similar entries here
]
]
我正在尝试对此地图访问进行排序。设置为属性的键=管道版本的值,格式为x.x,即4位数设置大小写。
我尝试了以下命令,但它没有给我包含1.0.0.1104作为PIPELINE_VERSION的条目。它给了我1.0.0.75 (看起来像某种字符串类型的排序。
// Sort the list entries acc. to pipeline version
def sortedList = list.sort { it.properties.PIPELINE_VERSION.value }
println "###### sortedList" + sortedList
println "\n^^^^\n"
println sortedList.last() // this should return me the entry which contains 1.0.0.1104 but I'm getting 1.0.0.75
}
我还尝试以def sortedList = list.sort { it.properties.PIPELINE_VERSION.toInteger().value }
的身份使用.toInteger(),但没有成功,并给出了一个错误。
17:07:22 Caught: groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.toInteger() is applicable for argument types: () values: []
17:07:22 Possible solutions: toUnique(), toUnique()
17:07:22 groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.toInteger() is applicable for argument types: () values: []
17:07:22 Possible solutions: toUnique(), toUnique()
已尝试:这两个都不起作用的list.sort {it.value.tokenize('.').last()}
。
以下是较小的示例:
map = ['a':'1.0.0.11', d:'1.0.0.85', 'b':'1.0.0.1104', 'c':"1.0.0.75"]
println " before sorting : " + map
//map = map.sort {it.value } // this doesn't work if the value is not a pure number format aka x.x.x. format ok lets try the following
map = map.sort {it.value.tokenize('.').last()} // cool that didn't work either
println " after sorting : " + map
问题:
发布于 2018-08-30 01:55:06
下面应该可以(假设格式X.X总是以X作为数字)
def sortClosure = { a, b ->
// Extract the pattern
def extract = {
it.properties.find { it.key == 'PIPELINE_VERSION' }?.value?.tokenize(/./)
}
// Transpose the numbers to compare
// gives [[1,1], [0,0], [0,0], [11, 1104]] for example
def transposed = [extract(a), extract(b)].transpose()
// Then compare the first occurrence of non-zero value (-1 or 1)
def compareInt = transposed.collect {
it[0].toInteger() <=> it[1].toInteger()
}.find()
compareInt ?: 0
}
list.sort(sortClosure)
发布于 2018-08-30 01:54:27
def versions = ['a':'1.0.0.11', d:'1.0.0.85', 'b':'1.0.0.1104', 'c':"1.0.0.75"]
//sort:
def sorted = versions.sort{ (it.value=~/\d+|\D+/).findAll() }
结果:
[a:1.0.0.11, c:1.0.0.75, d:1.0.0.85, b:1.0.0.1104]
发布于 2018-08-30 02:01:19
这个单行解决方案起作用了。
对于较小的示例!
def versions = ['a':'1.0.0.11', d:'1.0.0.85', 'b':'1.0.0.1104', 'c':"1.0.0.75"]
map = map.sort {it.value.tokenize('.').last().toInteger() }
好的,找到了复杂结构的shenzi(one-liner)解决方案(从dmahapatro的答案中得到提示):即一个映射>包含数组>包含另一个用于PIPELINE_VERSION的映射。
println "\n\n before sorting : " + list
list = list.sort {it.properties.find { it.key == 'PIPELINE_VERSION' }?.value?.tokenize('.').last().toInteger() }
println " after sorting : " + list
println "\n\n The last entry which contains the sorted shenzi is: " + map.last()
注意:上面的解决方案和到目前为止的其他答案,只有当管道的前3位集合是1.0.0时才会,即它只根据第四位集合(.last())决定最高的数字。使用类似的一行代码来找到最高的PIPELINE_VERSION会很有趣,它实际上涵盖了所有4或N否。一组数字。
https://stackoverflow.com/questions/52082751
复制相似问题