Java implements version comparison, and java implements version

Source: Internet
Author: User

Java implements version comparison, and java implements version

The version comparison function is often used in the client-related systems. However, the compareTo and other methods cannot be used completely in the string comparison mode;

In this case, we need to summarize the general rules of the version number, design a comparison algorithm, and encapsulate it into a general method for use:

The general versions are as follows: 1.3.20.8, 6.82.20160101, 8.5a/8.5c, etc;

The general rule is to split the version string by the dot, and then compare the main version with the main version. This version is compared with this version, so that the first-level and later versions are compared in order until there is a split size;

It is worth noting that many methods of comparing version numbers convert strings to int or double types first, which may not be applicable because it may contain letters such as 8.5c;

The common method is to compare the split string as a string. However, before comparing strings, compare the digits first;

Example of how to compare version numbers:

/*** Compare the version number. If the former is large, a positive number is returned. If the latter is large, a negative number is returned, returns 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); // obtain 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 character + + idx;} // If the size has been split, return directly. If the size is not split, then compare the number of digits. The number of subversions is large; diff = (diff! = 0 )? Diff: versionArray1.length-versionArray2.length; return diff ;}

Note: The input parameter of the split method is a regular expression and cannot be used ". "(". "match any value in the regular expression), use "\\. ", which is separated by point number;

In this way, the sub-string array is split into substrings, and the sub-version number is compared. When the sub-version number is compared, the number of digits is compared first. When the number of digits is the same, the sub-string comparison is performed;

After all the comparisons are completed (one of the versions is compared), let's take a look at which version has more sub-version numbers, that is, the split array length. A sub-version number can be larger than a large one;

In this way, we have comprehensively considered various situations and compared the size of the published document, including those with letter suffixes;

For example, "9.9", "10.8.8.6". If you compare the values by string directly, the former is large, the latter is small, and the latter is obviously incorrect; after the split, compare the first major version 9 and 10. From the number of digits, it is concluded that the latter is greater;

Another example is "9.9b", "9.8a", and so on. If the method is converted to int or double, it is not applicable;

Thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.