1 public abstract class Collator implements Comparator<Object>, Cloneable{}
1 /** 2 * Returns a {@code Collator} instance which is appropriate for the user's default 3 * {@code Locale}. 4 * See "<a href="../util/Locale.html#default_locale">Be wary of the default locale</a>". 5 */ 6 public static Collator getInstance() { 7 return getInstance(Locale.getDefault()); 8 }
1 /** 2 * Returns a {@code Collator} instance which is appropriate for {@code locale}. 3 */ 4 public static Collator getInstance(Locale locale) { 5 if (locale == null) { 6 throw new NullPointerException("locale == null"); 7 } 8 return new RuleBasedCollator(new RuleBasedCollatorICU(locale)); 9 }
1 public class CompareHelper { 2 3 public static final Collator COLLATOR = Collator.getInstance(); 4 5 public static final Comparator<Contact> COMPARATOR_CONTACT; 6 7 static 8 { 9 COMPARATOR_CONTACT = new Comparator<Contact>(){ 10 public final int compare(Contact a, Contact b){ 11 return COLLATOR.compare(a.sortKeyString, b.sortKeyString); 12 } 13 }; 14 } 15 private CompareHelper(){} 16 }
2.對List元素進行從新排序:html
1 Collections.sort(contacts, CompareHelper.COMPARATOR_CONTACT);
3.針對兩個字符串進行「本地化」比較,使用的方法是:java
int compareRet = CompareHelper.COLLATOR.compare(stringA, stringB);
不要使用String自帶的方法stringA.compareTo("stringB")。反之,當須要使用非「本地化」的比較方法時,須要使用的是stringA.compareTo("stringB")app