StringNumberComparator implements Comparator<String> { @Override public int compare(final String string1, final String string2) { final Iterator<String> iterator1 = new StringNumberPartIterator(string1); final Iterator<String> iterator2 = new StringNumberPartIterator(string2); while (iterator1.hasNext() && iterator2.hasNext()) { final String part1 = iterator1.next(); final String part2 = iterator2.next(); final int result; if (isNumber(part1) && isNumber(part2)) { result = Long.valueOf(part1).compareTo(Long.valueOf(part2)); } else { result = part1.compareTo(part2); } if (0 != result) { return result; } } return Integer.compare(string1.length(), string2.length()); } private  StringNumberComparator(); @Override int compare(final String string1, final String string2); static final Comparator<String> INSTANCE; }
@Test public void checkTestCase() throws Exception { assertEquals(result, StringNumberComparator.INSTANCE.compare(string1, string2)); }