The function of comparing version number is often needed in the system of the client, but comparing the version number can not use the method of CompareTo in the way of the string comparison completely;
This requires that we summarize the general rules of the version number, design a comparison algorithm and encapsulate it as a common method to use:
Usually version number such as: 1.3.20.8,6.82.20160101,8.5A/8.5C, etc.;
The general rule is that the version string is divided by the dot number, and then the major version compared to the main version, this version compared with this version, so that the order of a first-level comparison, until the size;
It is worth noting that many methods of comparing version numbers first convert the string to int or double type, which is not necessarily universal, because it may contain letters, such as the version number 8.5c;
The common way is still to compare the split string as a string, but before comparing the string, compare the number of digits;
Example of how to compare version numbers:
[Java]View PlainCopy
- /**
- * Compare the size of the version number, the former large return a positive value, the latter large return a negative, equal to 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 this is a regular match and cannot be used with "." ;
- string[] VersionArray2 = version2.split ("\ \");
- int idx = 0;
- int minLength = Math.min (Versionarray1.length, versionarray2.length); //Take Minimum length value
- int diff = 0;
- While (idx < minLength
- && (diff = versionarray1[idx].length ()-versionarray2[idx].length ()) = = 0//Compare length first
- && (diff = versionarray1[idx].compareto (versionarray2[idx)) = = 0) {//re-compare characters
- ++idx;
- }
- //If the size has been divided, then return directly, if the size is not, then compare the number of digits, there is a child version of the large;
- diff = (diff ! = 0)? diff:versionarray1.length-versionarray2.length;
- return diff;
- }
Note: Where the Split method entry is a regular match expression, you cannot use "." ("." Match any value in the regular expression, you need to use "\ \" To be divided by the point number;
In this way, first split into a substring array, and then compare the sub-version number, compare the sub-version number, the first comparison of the number of digits, the number of large, the same number of digits and then by comparison of the string comparison;
If all is compared (one version number is finished), then look at which version number has more sub-version number, that is, the length of the segmented array, there are sub-version number is large;
In this way, the various cases are considered well, and the size of the publication is compared, including those with the letter suffix;
such as "9.9", "10.8.8.6", if directly by the string comparison, then the former large, the latter small, and obviously wrong; After splitting the first major version 9 and 10, from the number of digits, it has been obtained the result of the latter large;
Again such as "9.9b", "9.8a" and so also applicable, if the conversion to int or double method is not applicable;
Please correct me!
http://blog.csdn.net/sowhat_ah/article/details/43955337
Java Implementation comparison version number