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