I am having the below object
List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
How do i sort this based on the value ("Object")
in the Map?
Create your own Comparator
and use it .
List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
Collections.sort(listMap,new Comparator<Map<String, Object>>() {
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
//your logic goes here
}
});
使用您自己的比较器(您必须实现给定的接口)
One of the easiest ways to solve using Java 8 is :
Collections.sort(listMap, Comparator.comparing(map -> (String)map.get("key")));
Or,
listMap = listMap.stream().sorted(Comparator.comparing(map -> (String)map.get("key"))).collect(Collectors.toList());
Note: I have converted key(Object) into String. You can use as per your requirements.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.