The function of comparing version numbers is often needed in systems involving client side, but comparing version numbers cannot be used completely according to string comparison methods such as compareTo;
This requires us to summarize the common rules of version numbers, design a comparison algorithm and encapsulate it into a general method to use:
The usual version numbers are: 1.3.20.8, 6.82.20160101, 8.5a/8.5c, etc.;
The general rule is to first divide the version string by dot number, then compare the main version with the main version, and compare this version with this version, so that it can be compared one by one in sequence until the size is divided;
It is worth noting that many methods of comparing version numbers convert the string to int or double type first, which may not be universal because it may contain letters, such as 8.5c;
The general way is to compare the divided string as a string, but before comparing the string, compare the number of bits first;
Example of a method to compare version numbers:
/** * Compare the size of the version number. If the former is large, return a positive number, the latter is large and returns a negative number, and if the same, return 0 * @param version1 * @param version2 * @return */ public static int compareVersion(String version1, String version2) throws Exception { if (version1 == null || version2 == null) { throw new Exception("compareVersion error:illegal params."); } String[] versionArray1 = version1.split("//.");//Note that this is a regular match, and "." cannot be used; String[] versionArray2 = version2.split("//."); int idx = 0; int minLength = Math.min(versionArray1.length, versionArray2.length);//Get the minimum length value int diff = 0; while (idx < minLength && (diff = versionArray1[idx].length() - versionArray2[idx].length()) == 0//Compare the length first&& (diff = versionArray1[idx].compareTo(versionArray2[idx])) == 0) {//Compare the characters++idx; } //If the size has been divided, it will be returned directly. If the size has not been divided, then compare the number of bits again. The sub-version of the number is large; diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length; return diff; } Note: The split method parameter is a regular matching expression, and cannot be used as "." ("." ("." to match any value in the regular expression). You need to use "//." to be considered to be split by point number;
In this way, first divide into substring arrays, and then compare subversion numbers one by one. When comparing subversion numbers, first compare the number of digits. If the number of digits is large, the number of digits will be large. When the number of digits is the same, then compare according to the string comparison method;
If all the comparisons are completed (one of the version numbers is compared), then look at which version number has more subversion numbers, that is, the length of the divided array, and the subversion numbers are larger;
This way, various situations are considered more thoroughly and the version number size is compared; including letter suffixes can also be used;
For example, "9.9", "10.8.8.6", if you compare directly by string, the former will be larger and the latter will be smaller, which is obviously wrong; after segmentation, the first major version 9 and 10 are compared, and from the digits, the result will be large;
For example, "9.9b", "9.8a", etc. are also applicable, but it is not applicable if the method of converting to int or double is used.