Map根据value排序
传进来一个map可以返回一个根据value排序的map
public static Map sortByValue(Map map) {
List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
return o1.getValue().compareTo(o2.getValue());//升序
//return o2.getValue().compareTo(o1.getValue()); 降序
}
});
Map newMap = new LinkedHashMap();
Iterator<Map.Entry<String, String>> iter = list.iterator();
Map.Entry tmpEntry = null;
while (iter.hasNext()) {
tmpEntry = iter.next();
newMap.put(tmpEntry.getKey(), tmpEntry.getValue());
}
return newMap;
}
List排序
Collections.sort(list);//升序
Collections.reverse(list);//降序