利用android sdk的提供的接口,可以完成入中文通訊錄的排序,請看一下代碼
@SuppressWarnings("unchecked")
public class ChineseCharComp implements Comparator {
public int compare(Object o1, Object o2) {
Collator myCollator = Collator.getInstance(java.util.Locale.CHINA);
if (myCollator.compare(o1, o2) < 0)
return -1;
else if (myCollator.compare(o1, o2) > 0)
return 1;
else
return 0;
}
}
ArrayList list = new ArrayList();
list.add("中國");
list.add("加拿大");
list.add("美國");
list.add("A");
list.add("more");
list.add("啊");
Comparator cmp = new ChineseCharComp();
Collections.sort(list, cmp);
Iterator iter = list.iterator();
while (iter.hasNext()) {
System.out.println( "—-" + iter.next());}