排序 -java

对象排序

  • 按Date降序
    static class Stu {
        String name;
        Date time;
        public Stu() {
        }
        public Stu(Date time) {
            this.time = time;
        }
        @Override
        public String toString() {
            return "Stu{" +
                    "name='" + name + '\'' +
                    ", time=" + time +
                    '}';
        }
    }
    public static void main(String[] args) {
        List list = new ArrayList<>();
        list.add(new Stu(new Date(System.currentTimeMillis())));
        list.add(new Stu(new Date(System.currentTimeMillis() + 200000L)));
        list.add(new Stu(new Date(System.currentTimeMillis() + 400000L)));
        list.sort((o1, o2) -> -o1.time.compareTo(o2.time));
        list.forEach(System.out::println);
    }

你可能感兴趣的:(排序 -java)