- 案例1:
代码实现:
package 黑马程序员;
import java.util.Arrays;
import java.util.Comparator;
public class text {
public static void main(String[] args) {
girl gf1 = new girl(18, "aa", 1.67);
girl gf2 = new girl(19, "bb", 1.7);
girl gf3 = new girl(20, "cc", 1.8);
girl[] girls={gf1,gf2,gf3};
Arrays.sort(girls, ( o1, o2) ->{
double temp=o1.getAge()-o2.getAge();
temp=temp==0?o1.getHeight()-o2.getHeight():temp;
temp=temp==0?o1.getName().compareTo(o2.getName()):temp;
if (temp>0){
return 1;
}else if (temp<0){
return -1;
} else {
return 0;
}
}
);
System.out.println(Arrays.toString(girls));
}
}
class girl{
private int age;
private String name;
private double height;
public girl(int age,String name, double height) {
this.age = age;
this.name = name;
this.height = height;
}
public girl() {
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}