Compare version strings in groovy

后端 未结 11 1929
耶瑟儿~
耶瑟儿~ 2020-11-30 09:50

Hey I have created a Groovy script that will extract the version numbers of some folder. I would then like to compare the version numbers and select the highest.

I g

11条回答
  •  一向
    一向 (楼主)
    2020-11-30 10:53

    String maxVersion(versions) {
        versions.max { a, b ->
            List verA = a.tokenize('.')
            List verB = b.tokenize('.')
            def commonIndices = Math.min(verA.size(), verB.size())
            for (int i = 0; i < commonIndices; ++i) {
                def numA = verA[i].toInteger()
                def numB = verB[i].toInteger()
                if (numA != numB) {
                    return numA <=> numB
                }
            }
            verA.size() <=> verB.size()
        }
    }
    

提交回复
热议问题