map排序,map转json防止顺序错乱

public static void main(String[] args) {
		 Map mapsort = new TreeMap(
	                new Comparator() {
	                    public int compare(String obj1, String obj2) {
	                        // 降序排序
	                        return obj2.compareTo(obj1);
	                    }
	                });
		 Map map = new HashMap();
		 map.put("2018-04", "123");
		 map.put("2018-01", "123");
		 map.put("2018-05", "123");
		 map.put("2018-02", "123");
		 map.put("2018-06", "123");
		 mapsort.putAll(map);
		 for (String key : mapsort.keySet()) { 
			  System.out.println("Key = " + key); 
			} 
	}
 //防止顺序错乱
mapsort=JSON.parseObject(mapsort.toString(),new TypeReference>(){} ,Feature.OrderedField);


你可能感兴趣的:(map排序)