谨以此文献给天上星
一 、通过code自然排序
resultList.sort((h1, h2) -> h1.getCode().compareTo(h2.getCode()));
二 、通过code反序排列
Comparator<ProomRealTimeOut> comparator = (h1, h2) -> h1.getCode().compareTo(h2.getCode());
resultList.sort(comparator.reversed());
注:换一下h1,h2顺序也可以实现反序
三 、通过code自然排序
Collections.sort(resultList, (v1, v2) -> v1.getCode().compareTo(v2.getCode()));
注:这种和本质上和第一种一样
四、通过code自然排序
List<ProomRealTimeOut> collect = resultList.stream().sorted((h1, h2) -> h1.getCode().compareTo(h2.getCode())).collect(Collectors.toList());
注:stream的排序不会操作原对象,必须收集排序结果。即collect为已排序List
以上方法均有无comparator参数的重载方法,方法默认使用List内对象的compare方法,即对象必须实现Comparable接口重写compareTo方法
############################################################################################################################################################################################
(h1, h2) -> h1.getCode().compareTo(h2.getCode())本质是通过Lambda 表达式实现了Comparator<T>函数式接口的compare方法,该方法有两个T型的入参即h1,h2,一个int型的出参即h1.getCode().compareTo(h2.getCode())