0.根據集合的對象或屬性 正序排序
0.1根據屬性
List<Goods> collect = goodsList.stream().sorted(Comparator.comparing(Goods::getAmunt)).collect(Collectors.toList());
0.2根據對象本身,適應於(List<Integer> 或 List<String>)
List<String> collect = list.stream().sorted(Comparator.comparing(Function.identity())).collect(Collectors.toList());
1.根據集合的對象的屬性(Integer)進行排序
1.1正序
List<Goods> collect = goodsList.stream()
.sorted((x,y) -> Integer.compare(x.getAmunt(),y.getAmunt()))
.collect(Collectors.toList());
1.2逆序
List<Goods> collect = goodsList.stream()
.sorted((x,y) -> Integer.compare(y.getAmunt(),x.getAmunt()))
.collect(Collectors.toList());
2.根據集合的對象的屬性(String)進行排序
2.1正序
List<Goods> collect = goodsList.stream()
.sorted((x,y) -> x.getName().compareTo(y.getName()))
.collect(Collectors.toList());
2.2 逆序
List<Goods> collect = goodsList.stream()
.sorted((x,y) -> y.getName().compareto(x.getName()))
.collect(Collectors.toList());