Java tutorial
//package com.java2s; //License from project: Open Source License import java.text.Collator; public class Main { public static final int compareNatural(String firstString, String secondString) { int firstIndex = 0; int secondIndex = 0; Collator collator = Collator.getInstance(); while (true) { if (firstIndex == firstString.length() && secondIndex == secondString.length()) { return 0; } if (firstIndex == firstString.length()) { return -1; } if (secondIndex == secondString.length()) { return 1; } if (Character.isDigit(firstString.charAt(firstIndex)) && Character.isDigit(secondString.charAt(secondIndex))) { int firstZeroCount = 0; while (firstString.charAt(firstIndex) == '0') { firstZeroCount++; firstIndex++; if (firstIndex == firstString.length()) { break; } } int secondZeroCount = 0; while (secondString.charAt(secondIndex) == '0') { secondZeroCount++; secondIndex++; if (secondIndex == secondString.length()) { break; } } if ((firstIndex == firstString.length() || !Character.isDigit(firstString.charAt(firstIndex))) && (secondIndex == secondString.length() || !Character.isDigit(secondString.charAt(secondIndex)))) { continue; } if ((firstIndex == firstString.length() || !Character.isDigit(firstString.charAt(firstIndex))) && !(secondIndex == secondString.length() || !Character.isDigit(secondString.charAt(secondIndex)))) { return -1; } if ((secondIndex == secondString.length() || !Character.isDigit(secondString.charAt(secondIndex)))) { return 1; } int diff = 0; do { if (diff == 0) { diff = firstString.charAt(firstIndex) - secondString.charAt(secondIndex); } firstIndex++; secondIndex++; if (firstIndex == firstString.length() && secondIndex == secondString.length()) { return diff != 0 ? diff : firstZeroCount - secondZeroCount; } if (firstIndex == firstString.length()) { if (diff == 0) { return -1; } return Character.isDigit(secondString.charAt(secondIndex)) ? -1 : diff; } if (secondIndex == secondString.length()) { if (diff == 0) { return 1; } return Character.isDigit(firstString.charAt(firstIndex)) ? 1 : diff; } if (!Character.isDigit(firstString.charAt(firstIndex)) && !Character.isDigit(secondString.charAt(secondIndex))) { if (diff != 0) { return diff; } break; } if (!Character.isDigit(firstString.charAt(firstIndex))) { return -1; } if (!Character.isDigit(secondString.charAt(secondIndex))) { return 1; } } while (true); } else { int aw = firstIndex; int bw = secondIndex; do { firstIndex++; } while (firstIndex < firstString.length() && !Character.isDigit(firstString.charAt(firstIndex))); do { secondIndex++; } while (secondIndex < secondString.length() && !Character.isDigit(secondString.charAt(secondIndex))); String as = firstString.substring(aw, firstIndex); String bs = secondString.substring(bw, secondIndex); int subwordResult = collator.compare(as, bs); if (subwordResult != 0) { return subwordResult; } } } } }