列表中每个版本的记录

时间:2018-11-15 11:40:47

标签: groovy

我有一个版本列表

1.0.0.1 - 10
1.1.0.1 - 10
1.2.0.1 - 10

那是我名单上的30零吉。但是我只想显示每种类型的5个最高nr:

1.0.0.5 - 10
1.1.0.5 - 10
1.2.0.5 - 10

我该怎么做?最后一个nr可以是任何数字,但只有三个第一个nr

1.0.0
1.1.0
1.2.0

代码:

import groovy.json.JsonSlurperClassic 

def data = new URL("http://xxxx.se:8081/service/rest/beta/components?repository=Releases").getText()  


/**
* 'jsonString' is the input json you have shown
* parse it and store it in collection
*/
Map convertedJSONMap = new JsonSlurperClassic().parseText(data)

def list = convertedJSONMap.items.version

list

1 个答案:

答案 0 :(得分:1)

仅靠版本号通常很难做到。所以我将它们分成数字,然后从那里开始工作。例如

def versions = [
"1.0.0.12", "1.1.0.42", "1.2.0.666",
"1.0.0.6", "1.1.0.77", "1.2.0.8",
"1.0.0.23", "1.1.0.5", "1.2.0.5",
]

println(
    versions.collect{ 
        it.split(/\./)*.toInteger()  // turn into array of integers
    }.groupBy{ 
        it.take(2) // group by the first two numbers
    }.collect{ _, vs -> 
        vs.sort().last() // sort the arrays and take the last
    }*.join(".") // piece the numbers back together
)
// => [1.0.0.23, 1.1.0.77, 1.2.0.666]