我有以下代碼進行排序列表,但我需要使它降序排序,改變升序排序降序
List list = new LinkedList(thismap.entrySet());
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o2)).getValue())
.compareTo(((Map.Entry) (o1)).getValue());
}
});
Map output = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
output.put(entry.getKey(), entry.getValue());
}
只需在'compare'中翻轉參數,其餘代碼就可以保持相同。 –