Groovy web console

subscribe to the feed Subscribe
to this
site

version-comparator

Published 2 months ago by Anonymous
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
def getLatestVersion(List versions) {
    def sortedVersions = versions.sort(false) { a, b -> 
        List verA = a.tokenize('.')
        List verB = b.tokenize('.')

        def commonIndex = Math.min(verA.size(), verB.size())
        for (int i = 0; i < commonIndex; i++) {
            def numA = verA[i].toInteger()
            def numB = verB[i].toInteger()
            if (numA != numB) {
                return numA <=> numB
            }
        }
        verA.size() <=> verB.size()
    }
    return sortedVersions[-1]
}

cypressMinVersion = "9.0.0"
v = ["9.0.0", "11.0.1", "0.7.1"]
recentVersion = "${getLatestVersion(v)}"
println "${recentVersion == cypressMinVersion ? 'Version is under 9.x.x' : 'Version is over 9.x.x'}"