List<Map>>按某个字段排序

//1、升序
CollectionUtil.sort(list,new MapComparatorAsc());

//2、降序
CollectionUtil.sort(list,new MapComparatorDesc());

 1、按id升序

    class MapComparatorAsc implements Comparator> {
        @Override
        public int compare(Map o1, Map o2) {
            String s1 = (String) o1.get("id");
            String s2 = (String) o2.get("id");
            if (s1 != null) {
                return id1.compareTo(s2);
            }
            return 0;
        }
    }

2、按时间降序

    class MapComparatorDesc implements Comparator> {
        @Override
        public int compare(Map o1, Map o2) {
            Timestamp t1 = (Timestamp) o1.get("updatedTime");
            Timestamp t2 = (Timestamp) o2.get("updatedTime");
            if (t2 != null) {
                return t2.compareTo(t1);
            }
            return 0;
        }
    }

你可能感兴趣的:(List,Map,字段排序)