一下代码为给一个hashMap的key排序,value排序一样处理
public static List<Map.Entry<String, Integer>> sortHashMapInteger(HashMap<String, Integer> map, final String dateFormat) {
List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
// 排序
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
//return ( o1.getKey()-o2.getKey());
String key1 = o1.getKey();
String key2 = o2.getKey();
Date day1 = null;
Date day2 = null;
try {
day1 = DateUtils.getDateByFormat(key1, dateFormat);
day2 = DateUtils.getDateByFormat(key2, dateFormat);
} catch (Exception e) {
e.printStackTrace();
}
return (day1.after(day2) == true ? 1 : 0);
}
});
return list;
}