Groovy字典映射 - 如何根据映射的键值进行排序 - 如果值为xxxx格式 - 用数字排序版本值。烧焦?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (407)

我在Groovy中有以下字典aka 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
   ]
]  

我正在尝试对此地图进行排序。to properties's key = PIPELINE_VERSION的值,格式为xxxx,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

问题:

  1. 如何获得具有最高PIPELINE_VERSION值的条目?
  2. 如何获取其值中包含最高PIPELINE_VERSOIN的第N个数组索引条目。
  3. 如何处理N没有。数字集案例?即1.0.0或1.2或1.0.0.12或1.4.1.9.255
提问于
用户回答回答于

下面应该工作(假设格式XXXX总是将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)
用户回答回答于
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]

所属标签

可能回答问题的人

  • 天使的炫翼

    17 粉丝531 提问5 回答
  • 找虫虫

    0 粉丝0 提问5 回答
  • 人生的旅途

    10 粉丝484 提问4 回答
  • 骑牛看晨曦

    4 粉丝522 提问4 回答

扫码关注云+社区

领取腾讯云代金券