Created
April 18, 2016 06:15
-
-
Save shau-lok/b226b2ad3b8bac728dab2feeb4c8f67a to your computer and use it in GitHub Desktop.
Android版本号比较
This file contains hidden or 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
public static int compareVersion(String version1, String version2) { | |
if (TextUtils.isEmpty(version1)) { | |
return -1; | |
} | |
if (TextUtils.isEmpty(version2)) { | |
return -1; | |
} | |
if (!TextUtils.isEmpty(version1) && !TextUtils.isEmpty(version2)) { | |
try { | |
String[] version1Array = version1.split("\\."); | |
String[] version2Array = version2.split("\\."); | |
int len = Math.min(version1Array.length, version2Array.length); | |
for (int i = 0; i < len; i++) { | |
int num1 = Integer.parseInt(version1Array[i]); | |
int num2 = Integer.parseInt(version2Array[i]); | |
if (num1 > num2) { | |
return 1; | |
} else if (num1 < num2) { | |
return -1; | |
} else { | |
continue; | |
} | |
} | |
return version1Array.length - version2Array.length; | |
} catch (Exception e) { | |
} | |
} | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment