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 got my script to run through the dir folder and I then get the versions in this format: 02.2.02.01
So I could get something like this:
- 02.2.02.01
- 02.2.02.02
- 02.2.03.01
I don't have them as a list but like this:
baseDir.listFiles().each { file ->
def string = file.getName().substring(5, 15)
// do stuff
}
Also I have tested that Groovy could compare them with the >
operator and it can! But now I need to select the one with the highest version
This appears to work
Here is an inadequate set of tests. You should add some more.
Run this code and the tests in the Groovy console to verify that it works
Here my solution:
If anyone is using Grails (e.g. Grails 2.2.3), I think VersionComparator already provides exactly what we need.
If you are not using Grails, you can always Google the source code of this class.
Example of working tests:
Hope this helps.
Here's a slightly modified version of Nikita's contribution:
If we're going for the shortest answer, this must come close ;-)