话不多说,直接上代码,根据苹果的重量排序,重量相等情况下根据国家排序
public static void main(String[] args){
List<Apple> appleList = new ArrayList<>();
Apple apple = new Apple("red","china","200");
Apple apple2 = new Apple("green","EngLand","150");
Apple apple3 = new Apple("red","Brazil","200");
appleList.add(apple);
appleList.add(apple2);
appleList.add(apple3);
System.out.println("appList sort before"+appleList.toString());
appleList.sort(comparing(Apple::getWeight)
.thenComparing(Apple::getCountry));
System.out.println("applist sort after"+appleList.toString());
}
处理之后结果:
如果觉得对你有帮助,请点个赞