使用方法
package com.sports.basketball.controller.dto;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class SortDataTest {
/**
* 根据属性升降序
*
* @param list
*/
public static void test1(List<entity> list) {
// 1、升序list.stream().sorted(Comparator.comparing(类::属性).reversed())
List<entity> result = list.stream().sorted(Comparator.comparing(entity::param).reversed())
.collect(Collectors.toList());
// 2、降序 list.stream().sorted(Comparator.comparing(类::属性一,Comparator.reverseOrder()))
List<entity> result2 = list.stream().sorted(Comparator.comparing(entity::param, Comparator.reverseOrder()))
.collect(Collectors.toList());
}
/**
* 以属性param升序,如果param值相同则以paramTwo降序
*
* @param list
*/
public static void test2(List<entity> list) {
// list.stream().sorted(Comparator.comparing(类::属性一).thenComparing(类::属性二));
list = list.stream().sorted(Comparator.comparing(entity::param).reversed()
.thenComparing(entity::paramTwo),Comparator.reverseOrder())
.collect(Collectors.toList());
System.out.println(JSON.toJSONString(list));
}
注:升序:Comparator.reverseOrder() 降序:.reversed()
}
标签:Comparator,stream,流中,list,param,comparing,sorted,Steam From: https://www.cnblogs.com/Gengzh/p/17215025.html