Mikael
Mikael

Reputation: 173

Records of each version in list

I have a list of versions

1.0.0.1 - 10
1.1.0.1 - 10
1.2.0.1 - 10

That is 30 nr in my list. But I only want to show the 5 highest nr of each sort:

1.0.0.5 - 10
1.1.0.5 - 10
1.2.0.5 - 10

How can I do that? The last nr can be any number but the 3 first nr is only

1.0.0
1.1.0
1.2.0

CODE:

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

Upvotes: 0

Views: 57

Answers (1)

cfrick
cfrick

Reputation: 37063

Version numbers alone usually don't make an easy sort. So I'd split them into numbers and work from there. E.g.

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]

Upvotes: 1

Related Questions