List中有map,需要对map中的value进行从小到大排序,使用lambda表达式可以轻松实现。
Collections.sort(list, (map1, map2) -> {
Integer value1= (Integer) map1.get("key");
Integer value2= (Integer) map2.get("key");
return value1.compareTo(value2);
});
System.out.println("排序后的list:"+list);
map1和map2的value进行对比,如果value是String类型,只需要把类型转换为String就行了。String的对比方式和Integer不一样。如果是String类型的数字,它排序规则是1,10,11....2,20,21...3,30,31,适合带有字母、汉字和数字的组合的排序,Integer直接就是按照数字实际大小进行排序。