最近坐到城市名的升序排序,记忆中可以通过使用pinyin4J来解决。但是参考其他前辈的博文,其实java已经提供接口来实现。简单的排序规则就不写,想实现中文String的升序功能,只要实现Comparator接口,并重写compare()方法来实现。
public class MyComparator implements Comparator {
private Collator mCollator;
public MyComparator() {
// TODO Auto-generated constructor stub
this.mCollator = Collator.getInstance();
}
@Override
public int compare(Object arg0, Object arg1) {
// TODO Auto-generated method stub
CollationKey formerKey = mCollator.getCollationKey(arg0.toString()
.trim());
CollationKey latterKey = mCollator.getCollationKey(arg1.toString()
.trim());
return formerKey.compareTo(latterKey);
}
}