@Test public void testVersion() { Assert.assertTrue(VersionCompareUtil.compareVersion("0.0.1", "0.0.0") > 0); Assert.assertEquals(0, VersionCompareUtil.compareVersion("0.0.0", "0.0.0")); Assert.assertTrue(VersionCompareUtil.compareVersion("0.0.0", "0.0.1") < 0); Assert.assertTrue(VersionCompareUtil.compareVersion("0.0.0", "0.0.0.0") < 0); Assert.assertTrue(VersionCompareUtil.compareVersion("0.0.1", "0.0.0.0") > 0); Assert.assertTrue(VersionCompareUtil.compareVersion("0.0.1", "0.0.0.0") > 0); }
public static int compareVersion(String version1, String version2) { if (version1 == null || version2 == null) { throw new RuntimeException("version can not be null"); } String[] versionArray1 = version1.split("\\."); String[] versionArray2 = version2.split("\\."); int idx = 0; int diff = 0; int minLength = Math.min(versionArray1.length, versionArray2.length); while (idx < minLength && versionArray1[idx].length() - versionArray2[idx].length() == 0 && versionArray1[idx].compareTo(versionArray2[idx]) == 0) { ++idx; } idx = idx < minLength ? idx : idx - 1; if (versionArray1[idx].length() - versionArray2[idx].length() == 0) { diff = versionArray1[idx].compareTo(versionArray2[idx]); } else { diff = versionArray1[idx].length() - versionArray2[idx].length(); } diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length; return diff; }
VersionCompareUtil { public static int compareVersion(String version1, String version2) { if (version1 == null || version2 == null) { throw new RuntimeException("version can not be null"); } String[] versionArray1 = version1.split("\\."); String[] versionArray2 = version2.split("\\."); int idx = 0; int diff = 0; int minLength = Math.min(versionArray1.length, versionArray2.length); while (idx < minLength && versionArray1[idx].length() - versionArray2[idx].length() == 0 && versionArray1[idx].compareTo(versionArray2[idx]) == 0) { ++idx; } idx = idx < minLength ? idx : idx - 1; if (versionArray1[idx].length() - versionArray2[idx].length() == 0) { diff = versionArray1[idx].compareTo(versionArray2[idx]); } else { diff = versionArray1[idx].length() - versionArray2[idx].length(); } diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length; return diff; } }
VersionCompareUtil { public static int compareVersion(String version1, String version2) { if (version1 == null || version2 == null) { throw new RuntimeException("version can not be null"); } String[] versionArray1 = version1.split("\\."); String[] versionArray2 = version2.split("\\."); int idx = 0; int diff = 0; int minLength = Math.min(versionArray1.length, versionArray2.length); while (idx < minLength && versionArray1[idx].length() - versionArray2[idx].length() == 0 && versionArray1[idx].compareTo(versionArray2[idx]) == 0) { ++idx; } idx = idx < minLength ? idx : idx - 1; if (versionArray1[idx].length() - versionArray2[idx].length() == 0) { diff = versionArray1[idx].compareTo(versionArray2[idx]); } else { diff = versionArray1[idx].length() - versionArray2[idx].length(); } diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length; return diff; }  }
VersionCompareUtil { public static int compareVersion(String version1, String version2) { if (version1 == null || version2 == null) { throw new RuntimeException("version can not be null"); } String[] versionArray1 = version1.split("\\."); String[] versionArray2 = version2.split("\\."); int idx = 0; int diff = 0; int minLength = Math.min(versionArray1.length, versionArray2.length); while (idx < minLength && versionArray1[idx].length() - versionArray2[idx].length() == 0 && versionArray1[idx].compareTo(versionArray2[idx]) == 0) { ++idx; } idx = idx < minLength ? idx : idx - 1; if (versionArray1[idx].length() - versionArray2[idx].length() == 0) { diff = versionArray1[idx].compareTo(versionArray2[idx]); } else { diff = versionArray1[idx].length() - versionArray2[idx].length(); } diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length; return diff; }  static int compareVersion(String version1, String version2); }
VersionCompareUtil { public static int compareVersion(String version1, String version2) { if (version1 == null || version2 == null) { throw new RuntimeException("version can not be null"); } String[] versionArray1 = version1.split("\\."); String[] versionArray2 = version2.split("\\."); int idx = 0; int diff = 0; int minLength = Math.min(versionArray1.length, versionArray2.length); while (idx < minLength && versionArray1[idx].length() - versionArray2[idx].length() == 0 && versionArray1[idx].compareTo(versionArray2[idx]) == 0) { ++idx; } idx = idx < minLength ? idx : idx - 1; if (versionArray1[idx].length() - versionArray2[idx].length() == 0) { diff = versionArray1[idx].compareTo(versionArray2[idx]); } else { diff = versionArray1[idx].length() - versionArray2[idx].length(); } diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length; return diff; }  static int compareVersion(String version1, String version2);  }