java stream流 排序

// 按状态排序,排序顺序是PipelineState枚举类的顺序
List newPipelineList = pipelineList.stream().sorted(Comparator.comparing(Pipeline::getState)).collect(Collectors.toList());
Collections.reverse(newPipelineList); //排序翻转

public enum PipelineState {
PENDING,
RUNNING,
ERROR,
DONE
}

上面的stream流将数组按PENDING RUNNING ERROR DONE顺序排序,转换PipelineState属性的顺序,排序也将改变

可以一直使用.thenComparing()来进行多字段排序

List newPipelineList = pipelineList.stream().sorted(Comparator.comparing(Pipeline::getState).thenComparing(Pipeline::getStartAt).thenComparing(Pipeline::getExecAt)).collect(Collectors.toList());

多字段排序

你可能感兴趣的:(java,java,spring)