1. 程式人生 > >java list 按照多字段排序

java list 按照多字段排序

升序 ets sed static sort class get des public

list 排序涉及的接口 Comparator, 具體實現代碼如下,以Apple類為例

public class Test{
  public static void main(String [] args){
     List<Apple> apples = new ArrayList<>(); Comparator<Apple> byName = Comparator.comparing(Apple::getName); Comparator<Apple> 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;
}

java list 按照多字段排序