AlphanumComparator implements Comparator<CharSequence> { @Override public int compare(CharSequence a, CharSequence b) { int lastEndA = 0; int lastEndB = 0; while (lastEndA < a.length() && lastEndB < b.length()) { int startA = findNextNumber(a, lastEndA); int startB = findNextNumber(b, lastEndB); if (startA == a.length() || startB == b.length()) { break; } if (lastEndA < startA || lastEndB < startB) { CharSequence subA = a.subSequence(lastEndA, startA); CharSequence subB = b.subSequence(lastEndB, startB); int c = collator.compare(subA, subB); if (c != 0) { return c; } } int endA = findNumberEnd(a, startA); int endB = findNumberEnd(b, startB); CharSequence subA = a.subSequence(startA, endA); CharSequence subB = b.subSequence(startB, endB); if (subA.length() > 9 || subB.length() > 9) { BigInteger ia = new BigInteger(subA.toString()); BigInteger ib = new BigInteger(subB.toString()); int c = ia.compareTo(ib); if (c != 0) { return c; } } else { Long ia = Long.parseLong(subA.toString()); Long ib = Long.parseLong(subB.toString()); int c = ia.compareTo(ib); if (c != 0) { return c; } } lastEndA = endA; lastEndB = endB; } if (lastEndA == 0 && lastEndB == 0) { return collator.compare(a, b); } if (lastEndA < a.length() || lastEndB < b.length()) { CharSequence subA = a.subSequence(lastEndA, a.length()); CharSequence subB = b.subSequence(lastEndB, b.length()); return collator.compare(subA, subB); } return 0; } AlphanumComparator(Locale locale); @Override int compare(CharSequence a, CharSequence b);  }
@Test public void compare() { AlphanumComparator c = new AlphanumComparator(new Locale("es")); assertEquals(0, c.compare("Hello", "Hello")); assertEquals(0, c.compare("Hello World", "Hello World")); assertEquals(-1, c.compare("Hello", "World")); assertEquals(1, c.compare("World", "Hello")); assertEquals(1, c.compare("World 1", "World")); assertEquals(-1, c.compare("World", "World 1")); assertEquals(-1, c.compare("Hello 1", "World")); assertEquals(0, c.compare("Hello 1", "Hello 1")); assertEquals(0, c.compare("Hello 1 World", "Hello 1 World")); assertEquals(-1, c.compare("Hello 1 World", "Hello 1 Zoo")); assertEquals(1, c.compare("Hello 10 World", "Hello 1 Zoo")); assertEquals(1, c.compare("Hello 1 World 10", "Hello 1 World 2")); assertEquals(1, c.compare("Hello 1 World 10 b", "Hello 1 World 10 a")); assertEquals(1, c.compare("axa 2", "aña 10")); assertEquals(-1, c.compare("aña 10", "axi 2")); assertEquals(-1, c.compare("ana 10", "aña 10")); assertEquals(1, c.compare("Tile 2", "File 10")); assertEquals(1, c.compare("Zoo 2", "File 10")); assertEquals(-1, c.compare("File 2", "File 10")); assertEquals(1, c.compare("File 10", "File 2")); assertEquals(-1, c.compare("File2", "File10")); assertEquals(1, c.compare("File10", "File2")); assertEquals(-1, c.compare("2", "File 10")); assertEquals(-1, c.compare("10", "File 2")); assertEquals(-1, c.compare("2", "10 File")); assertEquals(1, c.compare("10", "2 File")); assertEquals(-1, c.compare("2", "2 File")); assertEquals(1, c.compare("1 File", "1")); assertEquals(-1, c.compare("2", "File")); assertEquals(1, c.compare("File", "1")); assertEquals(-1, c.compare("Hello 2", "Hello File 10")); assertEquals(-1, c.compare("Hello 10", "Hello File 2")); assertEquals(-1, c.compare("2", "10")); assertEquals(1, c.compare("2", "1")); assertEquals(1, c.compare("-2", "2")); assertEquals(1, c.compare("-0.5", "-0.1")); assertEquals(1, c.compare("-0.5", "-0.1e1")); assertEquals(1, c.compare("1-2", "1-1")); assertEquals(1, c.compare("1 -2", "1 -1")); assertEquals(-1, c.compare("Manana", "Mañana")); }
@Test public void bigInt() { AlphanumComparator c = new AlphanumComparator(new Locale("es")); assertEquals(-1, c.compare("Hello 24", "Hello 7846785478595743")); assertEquals(-1, c.compare("Hello 7846785478595742", "Hello 7846785478595743")); }