List 中根据 Map<String,Object> 的某个值排序

  1. List<Map<String, String> resultList = new ArrayList<>();
  2. resultList.sort((o1, o2) -> {
  3. String key1 = o1.get("time");
  4. String key2 = o2.get("time");
  5. return key1.compareTo(key2);
  6. });

Collections.sort(list, comparator)

  1. public Comparator<Map<String, String>> mapComparator = new Comparator<Map<String, String>>() {
  2. public int compare(Map<String, String> m1, Map<String, String> m2) {
  3. return m1.get("name").compareTo(m2.get("name"));
  4. }
  5. }
  6. Collections.sort(list, mapComparator);

or

  1. Collections.sort(list, Comparator.comparing(map -> map.get("name")));

or

  1. list.sort(Comparator.comparing(map-> map.get("name")));

java8 lambda

  1. list = list.stream()
  2. .sorted((a,b)-> (a.get("name")).compareTo(b.get("name")))
  3. .collect(Collectors.toList());

or

  1. list = list.stream()
  2. .sorted(Comparator.comparing(map->map.get("name")))
  3. .collect(Collectors.toList());
返回笔记列表
入门小站