首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Groovy字典映射-如何根据映射的键值排序-如果值是x.x格式-按数字排序版本值。字符

Groovy字典映射-如何根据映射的键值排序-如果值是x.x格式-按数字排序版本值。字符
EN

Stack Overflow用户
提问于 2018-08-30 01:05:27
回答 4查看 597关注 0票数 1

我在Groovy中有以下字典,也就是MAP。

代码语言:javascript
复制
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 (看起来像某种字符串类型的排序。

代码语言:javascript
复制
// 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(),但没有成功,并给出了一个错误。

代码语言:javascript
复制
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()}

以下是较小的示例:

代码语言:javascript
复制
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

问题:

  1. 如何获取具有最高PIPELINE_VERSION值的条目?
  2. 如何获取其值中包含最高PIPELINE_VERSOIN的第N个数组索引条目。
  3. 如何处理N no。数字集集合的情况?即1.0.0、1.2、1.0.0.12或1.4.1.9.255
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-08-30 01:55:06

下面应该可以(假设格式X.X总是以X作为数字)

代码语言:javascript
复制
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)
票数 1
EN

Stack Overflow用户

发布于 2018-08-30 01:54:27

代码语言:javascript
复制
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() }

结果:

代码语言:javascript
复制
[a:1.0.0.11, c:1.0.0.75, d:1.0.0.85, b:1.0.0.1104]
票数 0
EN

Stack Overflow用户

发布于 2018-08-30 02:01:19

这个单行解决方案起作用了。

对于较小的示例!

代码语言:javascript
复制
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的映射。

代码语言:javascript
复制
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否。一组数字。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52082751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档