Is there a standard idiom for comparing version numbers? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be. I need to compare the versions and have the following hold true:
1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10
You need to normalise the version strings so they can be compared. Something like
Prints
The best to reuse existing code, take Maven's ComparableVersion class
advantages:
Don't include dependency to maven-artifact as that will pull various transitive dependencies
Tokenize the strings with the dot as delimiter and then compare the integer translation side by side, beginning from the left.
Wrote a little function myself.Simpler Using Lists
I created simple utility for comparing versions on Android platform using Semantic Versioning convention. So it works only for strings in format X.Y.Z (Major.Minor.Patch) where X, Y, and Z are non-negative integers. You can find it on my GitHub.
Method Version.compareVersions(String v1, String v2) compares two version strings. It returns 0 if the versions are equal, 1 if version v1 is before version v2, -1 if version v1 is after version v2, -2 if version format is invalid.
It's really easy using Maven:
You can get the right dependency string for Maven Artifact from this page: