public static int compareVersion(String v1, String v2) { //fast compare equals first if (v1.equals(v2)) { return 0; } String[] v1Comps = getVersionComponents(v1); String[] v2Comps = getVersionComponents(v2); int length = Math.max(v1Comps.length, v2Comps.length); for (int i = 0; i < length; i++) { Integer va = i < v1Comps.length ? Integer.parseInt(v1Comps[i]) : 0; Integer vb = i < v2Comps.length ? Integer.parseInt(v2Comps[i]) : 0; int compare = va.compareTo(vb); if (compare != 0) { return compare; } } return 0; }
public static int compareVersion(String v1, String v2) { //fast compare equals first if (v1.equals(v2)) { return 0; } String[] v1Comps = getVersionComponents(v1); String[] v2Comps = getVersionComponents(v2); int length = Math.max(v1Comps.length, v2Comps.length); for (int i = 0; i < length; i++) { Integer va = i < v1Comps.length ? Integer.parseInt(v1Comps[i]) : 0; Integer vb = i < v2Comps.length ? Integer.parseInt(v2Comps[i]) : 0; int compare = va.compareTo(vb); if (compare != 0) { return compare; } } return 0; }
public static int compareVersion(String v1, String v2) { //fast compare equals first if (v1.equals(v2)) { return 0; } Object[] v1Comps = getVersionComponents(v1); //1.2.3-hotfix -> [1, 2, 3, hotfix] Object[] v2Comps = getVersionComponents(v2); int index = 0; while (index < v1Comps.length && index < v2Comps.length) { int va = v1Comps[index] instanceof Integer ? (Integer)v1Comps[index] : VERY_LARGE_NUMBER; int vb = v2Comps[index] instanceof Integer ? (Integer)v2Comps[index] : VERY_LARGE_NUMBER; if (va != vb) { return va - vb; } if (va == VERY_LARGE_NUMBER) { // here, va and vb components must be same and Strings, compare as String int c = ((String)v1Comps[index]).compareTo((String)v2Comps[index]); if (c != 0) { return c; } } index++; } if (index < v1Comps.length) { // v1 is longer return 1; } //v2 is longer return -1; }