代码之家  ›  专栏  ›  技术社区  ›  Mikael

列表中每个版本的记录

  •  0
  • Mikael  · 技术社区  · 6 年前

    我有一个版本列表

    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可以是任何数字,但前3个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 回复  |  直到 6 年前
        1
  •  1
  •   cfrick    6 年前

    单凭版本号通常不容易排序。所以我把它们分成几个数字,然后从那里开始工作。例如。

    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]