Java Implementation comparison version number function _java

Source: Internet
Author: User
Tags diff

In the client-side system, it is often necessary to use the comparison version number, but comparing the version number can not be completely in accordance with the way the string comparison to use the CompareTo method;

This requires us to summarize the general rules of the version number, design a comparison algorithm and encapsulate it in a common way to use:

Usually version number such as: 1.3.20.8,6.82.20160101,8.5a/8.5c;

The common rule is to divide the version string by the point number, then compare the major version with the major version, compare it to this version, and then compare it to the first level of order, until the size is divided;

It is noteworthy that many methods of comparing version numbers first convert the string to an int or a double, 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 compare the digits before comparing the strings;

Examples of ways to compare version numbers:

/** * Compares the size of the version number, which returns a positive value, which returns a negative value and returns 0 * @param version1 * @param version2 * @return */public static int compareversion (string version1, String version2) throws Exception {if (Version1 = = Nu ll | | 
  Version2 = = null) {throw new Exception ("Compareversion error:illegal params."); } string[] VersionArray1 = Version1.split ("\."); /Note that 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//First compare length 
  && (diff = versionarray1[idx].compareto (versionarray2[idx])) = = 0) {//again to compare character ++idx; //If the size is already divided, return directly, if not the size, then compare the number of digits, with the sub version of the large; diff = (diff!= 0)? 
  Diff:versionarray1.length-versionarray2.length; 
return diff; } 

Note: The Split method entry is a regular matching expression and cannot be used with "." ("." To match any value in a regular expression, you need to use "\." To be divided by the point number;

In this way, the first segment into a substring array, and then compare the sub version number, compare the version number, first compare the number of digits, the number of large digits, the same as the number of digits in the same string comparison;

If all is finished (one version number is complete), look at which version number has a more sub version number, that is, the length of the segmented array, the size of the sub version number is large;

This makes it more perfect to consider the various situations, and compare the publication of the size of this number, including the letter suffix can also be used;

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 the split comparison of the first major version 9 and 10, from the number of digits, it has been found that the latter large;

Again, such as "9.9b", "9.8a" and so on, if the conversion to an int or double method does not apply.

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.