对LinkedHashMap按key进行降序排序 public static void main(String[] args) { LinkedHashMap<String,String> map = new LinkedHashMap<String,String>(); map.put("1","aaa"); map.put("3","bbb"); map.put("2","ccc"); map.put("5","ddd"); List<Map.Entry<String, String>> infoIds =new ArrayList<Map.Entry<String, String>>(map.entrySet()); //排序 Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() { public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) { String p1 = o1.getKey(); String p2 = o2.getKey();; return Integer.valueOf(p2)-Integer.valueOf(p1);//如果要升序, 改为return Integer.valueOf(p1)-Integer.valueOf(p2); } }); //转换成新map输出 LinkedHashMap<String, String> newMap = new LinkedHashMap <String, String>(); for(Map.Entry<String,String> entity : infoIds){ newMap.put(entity.getKey(), entity.getValue()); System.out.println(entity.getKey()); } } debug后的截图,已经降序排好 
|