@Test public void testNoc() { Assert.assertEquals(noc.compare("0a", "1"), -1); Assert.assertEquals(noc.compare("0", "1"), -1); Assert.assertEquals(noc.compare("1", "10"), -1); Assert.assertEquals(noc.compare("9", "10"), -1); Assert.assertEquals(noc.compare("a", "b"), -1); Assert.assertEquals(noc.compare("a9", "a10"), -1); Assert.assertEquals(noc.compare("0.9", "0.91"), -1); Assert.assertEquals(noc.compare("0.90", "0.91"), -1); Assert.assertEquals(noc.compare("1.2.x", "1.09.x"), -1); Assert.assertEquals(noc.compare("0", "1a"), -1); Assert.assertEquals(noc.compare("0a", "1"), -1); }
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; }