javacomparisonversioning

How do you compare two version Strings in Java?


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

Solution

  • Tokenize the strings with the dot as delimiter and then compare the integer translation side by side, beginning from the left.