【发布时间】:2018-08-29 17:05:27
【问题描述】:
我在 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
]
]
我正在尝试对这张地图进行排序。 properties's key = PIPELINE_VERSION's value,格式为x.x.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
}
还尝试将 .toInteger() 用作def sortedList = list.sort { it.properties.PIPELINE_VERSION.toInteger().value },但没有奏效,出现错误。
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
问题:
- 如何获取具有最高 PIPELINE_VERSION 值的条目?
- 如何获取第 N 个数组索引条目,它的值包含最高的 PIPELINE_VERSOIN。
- 如何处理 N 号。数字集集案例?即 1.0.0 或 1.2 或 1.0.0.12 或 1.4.1.9.255
【问题讨论】:
-
:) 差不多,但不准确,因为它没有处理相同的结构,而且解决方案看起来很长。下面提到的一个 dmahapatro 看起来更干净,但我试图在我的回答中让它更干净(单线)。
-
是的,距离不够近,无法关闭????希望它有助于比较版本
标签: arrays sorting dictionary groovy version