项目中用到一个对于Map排序的。下面贴出代码:
package org.hsyd.test;
import java.text.CollationKey;
import java.text.Collator;
import java.util.Comparator;
/**
* 对treeMap排序
* @author Administrator
*
*/
public class CollatorComparator implements Comparator {
Collator collator = Collator.getInstance();
public int compare(Object element1, Object element2) {
CollationKey key1 = collator.getCollationKey(element1.toString());
CollationKey key2 = collator.getCollationKey(element2.toString());
return key1.compareTo(key2);
}
}
//这样引用:
CollatorComparator comparator = new CollatorComparator();
TreeMap<String, HashMap<Long, Integer>> map=new TreeMap<String, HashMap<Long,Integer>>(comparator);