For strings, a combination of numbers and letters, such as "a20" and "a9"; the result is "a20">"a9". In this case, the result of calling String's compareTo method is not what we hope. At this time, you need to write your own class and implement the Commarable interface to compare. This is an interview question from the first time, but there was only one idea at that time.
Ideas:
1. Convert String to a list containing substring and Integer. (I don’t know how to split it directly using String’s spit method here?)
2. Compare the list data.
3. If the comparison results in the list are equal, then call String's compareTo.
Code implementation:
package testsource; import java.util.ArrayList; import java.util.List; /** * * @author Waston Xu * @date 2011-4-15 10:48:21 am */ public class MyString implements Comparable<MyString> { private final String string; private List<Object> list; public MyString(String string) { this.string = string; trimString2List(); } private void trimString2List() { list = new ArrayList<Object>(); Integer num = 0; StringBuffer sb = new StringBuffer(); for (int i = 0; i < this.string.length(); i++) { char c = this.string.charAt(i); if (c > 47 && c < 58) { if (sb.length() != 0) { list.add(sb); sb = new StringBuffer(); } num = num * 10 + (c - '0'); continue; } else { if (num != 0) { list.add(num); num = 0; } sb.append(c); continue; } } if (sb.length() != 0) { list.add(sb); sb = new StringBuffer(); } else if (num != 0) { list.add(num); num = 0; } } /* * After carefully watching String's compareTo method, I originally planned not to call its API for programming, * However, considering the occurrence of "a02" and "a2" cases, I still use the methods in its API to solve it. */ private int compareToLikeString(String s) { int len1 = string.length(); int len2 = s.length(); int n = Math.min(len1, len1); if (n > 0) { int k = 0; while (k < n) { char c1 = string.charAt(k); char c2 = s.charAt(k); if (c1 != c2) return c1 - c2; k++; } } return len1 - len2; } @Override public int compareTo(MyString anotherString) { int len1 = list.size(); int len2 = anotherString.list.size(); int n = Math.min(len1, len2); int mark = 0; if (n > 0) { int i = 0; while (i < n) { Object o1 = list.get(i); Object o2 = anotherString.list.get(i); if (o1 instanceof Integer && o2 instanceof Integer) { mark = (Integer) o1 - (Integer) o2; } else { mark = o1.toString().compareTo(o2.toString()); } if (mark != 0) return mark; i++; } } return compareToLikeString(anotherString.string); //return string.compareTo(anotherString.string); /* If you write this way, there will be a disadvantage that "a02" and "a2", it must be the previous small */ //return string.length() - anotherString.string.length(); } public static void main(String[] args) { String s1 = "b9c"; String s2 = "b09c"; MyString m1 = new MyString(s1); MyString m2 = new MyString(s2); System.out.println(m1.compareTo(m2)); } }To view more Java syntax, you can follow: "Thinking in Java Chinese Manual", "JDK 1.7 Reference Manual Official English Version", "JDK 1.6 API java Chinese Reference Manual", "JDK 1.5 API java Chinese Reference Manual". I also hope that everyone will support Wulin.com more.