全在代码里!! 超详细注释,可直接运行。
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
/**
* java中Comparator自定义排序:
* Comparator接口可以实现自定义排序,实现Comparator接口时,要重写compare方法:
* int compare(Object o1, Object o2) 返回一个基本类型的整型
* 如果要按照升序排序,则o1 小于o2,返回-1(负数),相等返回0,01大于02返回1(正数)
* 如果要按照降序排序,则o1 小于o2,返回1(正数),相等返回0,01大于02返回-1(负数)
* 我的理解:-1(负数)代表从左到右,+1(正数)代表从右到左。 从左到右(即从o1到o2)
* **定义一个Person类,属性为姓名name,学号number,年龄age,性别sex,身高height,体重weigth,自定义排序规则为:
* 性别男优先
* 年龄从小到大
* 体重从低到高
* 身高从高到低
* 姓名字数从少到多**
*/
class Person{
public String name;
public String id;
public int age;
public String sex;
public double height;
public double weight;
public Person(String name,String id,int age,String sex,double height,double weight){
this.name = name;
this.id = id;
this.age = age;
this.sex = sex;
this.height = height;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;