@Test public void testVersionNumbers() { Assert.assertEquals(0, noc.compare("10.5.8", "10.5.8")); Assert.assertTrue(noc.compare("10.5", "9.9") > 0); Assert.assertTrue(noc.compare("10.5.1", "10.5") > 0); Assert.assertTrue(noc.compare("10.5.1", "10.6") < 0); Assert.assertTrue(noc.compare("10.5.1-1", "10.5.1-0") > 0); }
public int compare(String a, String b) { int ia = 0, ib = 0; int nza = 0, nzb = 0; char ca, cb; int result; while (true) { nza = nzb = 0; ca = charAt(a, ia); cb = charAt(b, ib); while (Character.isSpaceChar(ca) || ca == '0') { if (ca == '0') { nza++; } else { nza = 0; } ca = charAt(a, ++ia); } while (Character.isSpaceChar(cb) || cb == '0') { if (cb == '0') { nzb++; } else { nzb = 0; } cb = charAt(b, ++ib); } if (Character.isDigit(ca) && Character.isDigit(cb)) { if ((result = compareRight(a.substring(ia), b.substring(ib))) != 0) { return result; } } else if ((Character.isDigit(ca) || nza>0) && (Character.isDigit(cb) || nzb>0)) { if (nza==0) { return 1; } if (nzb==0) { return -1; } } if (ca == 0 && cb == 0) { return nza - nzb; } if (ca < cb) { return -1; } else if (ca > cb) { return +1; } ++ia; ++ib; } }
NaturalOrderComparator implements Comparator<String> { public int compare(String a, String b) { int ia = 0, ib = 0; int nza = 0, nzb = 0; char ca, cb; int result; while (true) { nza = nzb = 0; ca = charAt(a, ia); cb = charAt(b, ib); while (Character.isSpaceChar(ca) || ca == '0') { if (ca == '0') { nza++; } else { nza = 0; } ca = charAt(a, ++ia); } while (Character.isSpaceChar(cb) || cb == '0') { if (cb == '0') { nzb++; } else { nzb = 0; } cb = charAt(b, ++ib); } if (Character.isDigit(ca) && Character.isDigit(cb)) { if ((result = compareRight(a.substring(ia), b.substring(ib))) != 0) { return result; } } else if ((Character.isDigit(ca) || nza>0) && (Character.isDigit(cb) || nzb>0)) { if (nza==0) { return 1; } if (nzb==0) { return -1; } } if (ca == 0 && cb == 0) { return nza - nzb; } if (ca < cb) { return -1; } else if (ca > cb) { return +1; } ++ia; ++ib; } } }
NaturalOrderComparator implements Comparator<String> { public int compare(String a, String b) { int ia = 0, ib = 0; int nza = 0, nzb = 0; char ca, cb; int result; while (true) { nza = nzb = 0; ca = charAt(a, ia); cb = charAt(b, ib); while (Character.isSpaceChar(ca) || ca == '0') { if (ca == '0') { nza++; } else { nza = 0; } ca = charAt(a, ++ia); } while (Character.isSpaceChar(cb) || cb == '0') { if (cb == '0') { nzb++; } else { nzb = 0; } cb = charAt(b, ++ib); } if (Character.isDigit(ca) && Character.isDigit(cb)) { if ((result = compareRight(a.substring(ia), b.substring(ib))) != 0) { return result; } } else if ((Character.isDigit(ca) || nza>0) && (Character.isDigit(cb) || nzb>0)) { if (nza==0) { return 1; } if (nzb==0) { return -1; } } if (ca == 0 && cb == 0) { return nza - nzb; } if (ca < cb) { return -1; } else if (ca > cb) { return +1; } ++ia; ++ib; } }  }
NaturalOrderComparator implements Comparator<String> { public int compare(String a, String b) { int ia = 0, ib = 0; int nza = 0, nzb = 0; char ca, cb; int result; while (true) { nza = nzb = 0; ca = charAt(a, ia); cb = charAt(b, ib); while (Character.isSpaceChar(ca) || ca == '0') { if (ca == '0') { nza++; } else { nza = 0; } ca = charAt(a, ++ia); } while (Character.isSpaceChar(cb) || cb == '0') { if (cb == '0') { nzb++; } else { nzb = 0; } cb = charAt(b, ++ib); } if (Character.isDigit(ca) && Character.isDigit(cb)) { if ((result = compareRight(a.substring(ia), b.substring(ib))) != 0) { return result; } } else if ((Character.isDigit(ca) || nza>0) && (Character.isDigit(cb) || nzb>0)) { if (nza==0) { return 1; } if (nzb==0) { return -1; } } if (ca == 0 && cb == 0) { return nza - nzb; } if (ca < cb) { return -1; } else if (ca > cb) { return +1; } ++ia; ++ib; } }  int compare(String a, String b); }
NaturalOrderComparator implements Comparator<String> { public int compare(String a, String b) { int ia = 0, ib = 0; int nza = 0, nzb = 0; char ca, cb; int result; while (true) { nza = nzb = 0; ca = charAt(a, ia); cb = charAt(b, ib); while (Character.isSpaceChar(ca) || ca == '0') { if (ca == '0') { nza++; } else { nza = 0; } ca = charAt(a, ++ia); } while (Character.isSpaceChar(cb) || cb == '0') { if (cb == '0') { nzb++; } else { nzb = 0; } cb = charAt(b, ++ib); } if (Character.isDigit(ca) && Character.isDigit(cb)) { if ((result = compareRight(a.substring(ia), b.substring(ib))) != 0) { return result; } } else if ((Character.isDigit(ca) || nza>0) && (Character.isDigit(cb) || nzb>0)) { if (nza==0) { return 1; } if (nzb==0) { return -1; } } if (ca == 0 && cb == 0) { return nza - nzb; } if (ca < cb) { return -1; } else if (ca > cb) { return +1; } ++ia; ++ib; } }  int compare(String a, String b); static final NaturalOrderComparator INSTANCE; }