Java Implementation comparison version number

Source: Internet
Author: User
Tags diff

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
  1. /**
  2. * Compare the size of the version number, the former large return a positive value, the latter large return a negative, equal to return 0
  3. * @param version1
  4. * @param version2
  5. * @return
  6. */
  7. Public static int compareversion (string version1, String version2) throws Exception {
  8. if (version1 = = Null | | version2 = = null) {
  9. throw New Exception ("compareversion error:illegal params.");
  10. }
  11. string[] VersionArray1 = version1.split ("\ \"); //Note this is a regular match and cannot be used with "." ;
  12. string[] VersionArray2 = version2.split ("\ \");
  13. int idx = 0;
  14. int minLength = Math.min (Versionarray1.length, versionarray2.length); //Take Minimum length value
  15. int diff = 0;
  16. While (idx < minLength
  17. && (diff = versionarray1[idx].length ()-versionarray2[idx].length ()) = = 0//Compare length first
  18. && (diff = versionarray1[idx].compareto (versionarray2[idx)) = = 0) {//re-compare characters
  19. ++idx;
  20. }
  21. //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;
  22. diff = (diff ! = 0)? diff:versionarray1.length-versionarray2.length;
  23. return diff;
  24. }

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

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.