java list 按照多字段排序

list 排序涉及的接口 Comparator, 具体实现代码如下,以Apple类为例

 

public class Test{
  public static void main(String [] args){
     List apples = new ArrayList<>(); Comparator byName = Comparator.comparing(Apple::getName); Comparator bySizeDesc = Comparator.comparing(Apple::getSize).reversed();
     // 先以名称升序排列,再按照size倒叙排列
     apples.sort(byName.thenComparing(bySizeDesc));   
  }
} @Data
public class Apple{ private String color; private String name; private Integer size;
}

 

转载于:https://www.cnblogs.com/wanglg629/p/10638954.html

你可能感兴趣的:(java list 按照多字段排序)