Comparison of software versions -- Comparison of Java Algorithm Implementation Software Versions

Source: Internet
Author: User

Comparison of software versions -- Comparison of Java Algorithm Implementation Software Versions

Recently, I encountered a small problem in development. The software version is compared: for example, 2.12.3 and 2.2.1.

In fact, the Client Version is updated to check whether there are any updates available in the background. The simplest method in Java is to obtain the current Client Version Number and compare it with the latest version number provided by the server.

Assume that the current client version is localVersion and the latest client version is onlineVersion.

If (localVersion. equals (onlineVersion) can be used to determine whether a new version exists. This kind of judgment is only done by comparing whether the local version number string and the background version number are exactly the same. However, there is a problem. If the background version is earlier than the current version. equals (onlineVersion) is also true, which will cause a problem. The software will be updated to a version earlier than the current version. Obviously this is not what we expected.

So how can we avoid this problem? We 'd better judge the version number. The original method is to convert the version number to double type data for comparison. For example, if the version number is 1.2.2, the first data in the string is replaced. all. double. the valueOf (String) method is converted to a value of the Double type for comparison. You can use this method to compare the version number. For example, the version numbers 1.2.2 and 1.2.3 compare the sizes of 1.22 and 1.23. Obviously, the comparison result is that the version number 1.2.3 is later than 1.2.2, and the result is correct.

 

/*** Determine whether it is the latest version ** @ param localVersion * local version * @ param onlineVersion * online version */public static boolean isNewVersionDouble (String localVersion, String onlineVersion) {if (localVersion = null | onlineVersion = null) {return false;} return parseStrVersionCodeToDouble (onlineVersion ). compareTo (parseStrVersionCodeToDouble (localVersion)> 0? True: false;}/*** convert String version to Double version ** @ param versionCode * @ return */private static Double parseStrVersionCodeToDouble (String versionCode) {if (versionCode. contains (.)) {String versionStr = versionCode. substring (0, versionCode. indexOf (.) + 1) + versionCode. substring (versionCode. indexOf (.) + 1 ). replace (.,); return Double. valueOf (versionStr);} else {return Double. valueOf (versionCode );}}

 

Later, we can use the String. compareTO Method for comparison on the Internet, and then try to directly make onlineVersion. compareTo (localVersion) achieve the same effect as converting to the double type.

 

/*** String. comparTo method ** @ param localVersion * @ param onlineVersion * @ return */public static boolean isNewVersionStr (String localVersion, String onlineVersion) {if (localVersion = null | onlineVersion = null) {return false;} return onlineVersion. compareTo (localVersion)> 0? True: false ;}

However, when comparing the version numbers (for example, 2.2.1 and 2.3.1) between two vertices, the above two methods return the correct results, however, an error is returned when comparing not all vertices in number (for example, 2.2.1 and 2.12.1). Obviously, the version 2.12.1 must be later than 2.2.1, but the returned results are the opposite. Why? When we use the double-type comparison method, we actually compare the values 2.21 and 2.121. Obviously, 2.21 is greater than 2.121. This method returns an incorrect result, which is not hard to understand. Use String. the compareTo method compares the Unicode value values of each character in the string in sequence, obviously, when the value is 2> 1 after the first decimal point, the error result of version 2.2.1 is greater than 2.12.1 is obtained.

 

So far, we can draw a conclusion that we can directly use String. the compareTo method can achieve the same effect of Double conversion. Therefore, the first method for double conversion and comparison has no meaning, but it is more troublesome and inefficient. However, we cannot use the String. compareTo method to compare the multiple-digit version numbers between two points. How can we return the correct results under any circumstances. One thing I think of is to use the dot splitting version number to compare the values of the array in sequence, so that the comparison considers 2 to be bigger than 12.

 

/*** Determine whether the version is the latest version. split into int array comparison ** @ param localVersion * local version * @ param onlineVersion * online version * @ return */public static boolean isAppNewVersion (String localVersion, String onlineVersion) {if (localVersion. equals (onlineVersion) {return false;} String [] localArray = localVersion. split (\.); string [] onlineArray = onlineVersion. split (\.); int length = localArray. length <onlineArray. length? LocalArray. length: onlineArray. length; for (int I = 0; I <length; I ++) {if (Integer. parseInt (onlineArray [I])> Integer. parseInt (localArray [I]) {return true;} else if (Integer. parseInt (onlineArray [I]) <Integer. parseInt (localArray [I]) {return false;} // compare the next group of values with equal values} return true ;}


 

The last method returns the correct result when comparing multiple version numbers between two points. If you are interested, copy the code and run it.

 

 

 

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.