Skip to content

Instantly share code, notes, and snippets.

@plombardi89
Created September 5, 2014 02:03
Show Gist options
  • Save plombardi89/b856c880e96fad5be543 to your computer and use it in GitHub Desktop.
Save plombardi89/b856c880e96fad5be543 to your computer and use it in GitHub Desktop.
Given a list of versions then determine the latest
def latestVersion(versions) {
def sorted = versions.sort(false) { a, b ->
a = a.split('\\.')
b = b.split('\\.')
// determines how many numbers need to be compared
def commonIndices = Math.min(a.size(), b.size())
// compare each number in a version
for (int idx = 0; idx < commonIndices; ++idx) {
def componentA = a[idx] as int
def componentB = b[idx] as int
if (componentA != componentB) {
return componentA <=> componentB
}
}
// if we escaped the loop, they are identical
a.size() <=> b.size()
}
// the last item is the latest
sorted[-1]
}
assert latestVersion(["1.0", "2.0", "3.0", "4.0"]) == "4.0"
assert latestVersion(["1.0", "1.0.1", "1.1.1", "0.0.0.1", "0.1.1", "1.1.0", "1.0.3.4.5.60024"]) == "1.1.1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment