Last active
August 29, 2015 13:56
-
-
Save taomaree/9029485 to your computer and use it in GitHub Desktop.
compare version of ver1 and ver2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* compare version of ver1 and ver2 | |
* | |
* @param ver1 | |
* @param ver2 | |
* @return | |
* 0: when ver1 equals ver2 <br> | |
* 1: when ver1 Minor or Patch version less than ver2 <br> | |
* 2: when ver1 Major version less than ver2 <br> | |
*/ | |
public static int compareVersion(String ver1, String ver2) { | |
int i = 0; | |
// support Semantic Versioning | |
String regex = "(\\w+)\\.(\\w+)\\.(\\w+)(.*)"; | |
Matcher m1 = Pattern.compile(regex).matcher(ver1); | |
Matcher m2 = Pattern.compile(regex).matcher(ver2); | |
if (m1.matches() && m2.matches()) { | |
int i1 = m2.group(1).compareToIgnoreCase(m1.group(1)); | |
int i2 = m2.group(2).compareToIgnoreCase(m1.group(2)); | |
int i3 = m2.group(3).compareToIgnoreCase(m1.group(3)); | |
if (i1 >= 1) | |
i = 2; | |
if (i1 == 0 && i2 >= 1) | |
i = 1; | |
if (i1 == 0 && i2 == 0 && i3 >= 1) | |
i = 1; | |
} | |
return i; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment