public class CollectionsSortDemo {
public static void main(String[] args) {
ArrayList<Map<String, Object>> mapList = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
Map<String, Object> map = Maps.newHashMap();
map.put("id", (int) (Math.random() * 100));// [0, 100)随机数
mapList.add(map);
}
Collections.sort(mapList, (o1, o2) -> {
Integer o1Value = Integer.valueOf(o1.get("id").toString());
Integer o2Value = Integer.valueOf(o2.get("id").toString());
return o1Value.compareTo(o2Value);// 升序
// return o2Value.compareTo(o1Value);// 降序
});
System.out.println(mapList.toString());// [{id=7}, {id=12}, {id=15}, {id=18}, {id=20}, {id=48}, {id=52}, {id=59}, {id=73}, {id=98}]
// Collections.sort(mapList, new Comparator<Map<String, Object>>() {
// @Override
// public int compare(Map<String, Object> o1, Map<String, Object> o2) {
// Integer o1Value = Integer.valueOf(o1.get("id").toString());
// Integer o2Value = Integer.valueOf(o2.get("id").toString());
// return o1Value.compareTo(o2Value);// 升序
// }
// });
}
}