当前位置: 首页 > 工具软件 > comparator > 使用案例 >

java中Comparator自定义排序

訾旭
2023-12-01

全在代码里!! 超详细注释,可直接运行。

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;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", id='" + id + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", height=" + height +
                ", weight=" + weight +
                '}';
    }
}


public class PersonTest {
    public static void main(String[] args) {
        System.out.println("男".compareTo("女"));
        List<Person> list=new LinkedList<Person>();
        list.add(new Person("钟爱", "201508020114", 21, "女", 162.01, 80.03));
        list.add(new Person("张大旗", "20150802", 22, "女", 159.99, 102.34));
        list.add(new Person("王大拿里哦", "20150802012", 20, "男", 182.01, 132));
        list.add(new Person("张三", "201501", 18, "男", 144, 112));
        list.add(new Person("张原野", "201505", 19, "女", 120, 144));
        list.add(new Person("李冬梅子", "201503", 20, "男", 189, 122));
        list.add(new Person("田园圣诞", "201504",20, "男", 189, 132));
        list.add(new Person("李很帅", "201502", 17, "男", 173, 132));

        System.out.println("排序前:");
        for (Person person : list) {
            System.out.println(person);
        }
        System.out.println("========================================================================");
//        Java中的compareTo()方法,返回参与比较的前后两个字符串的ASCII码的差值
//        对于String compareTo()方法:https://blog.csdn.net/FJJ543/article/details/99728140
//        我的理解:-1(负数)代表从左到右,+1(正数)代表从右到左。 从左到右(即从o1到o2)
        list.sort(new Comparator<Person>() {
            int flag=0;
            @Override
            public int compare(Person o1, Person o2) {
                int a=o1.getSex().compareTo(o2.getSex());
                if(a!=0&&a>0){
                    return -1;
                }
                else if(o1.getSex().equals(o2.getSex())){
                    if(o1.getAge()<o2.getAge()){
                        return -1;
                    }
                    else if(o1.getAge()==o2.getAge()){
                        if(o1.getWeight()<o2.getWeight()){
                            return -1;
                        }
                        else if(o1.getWeight()==o2.getWeight()){
                            if(o1.getHeight()>o2.getHeight()){
                                return -1;
                            }
                            else if(o1.getHeight()==o2.getHeight()){
                                if(o1.getName().length()<o2.getName().length()){
                                    return -1;
                                }
                            }
                        }
                    }
                }
                return 1;
            }
        });
        System.out.println("排序后:");
        for (Person person1 : list) {

            System.out.println(person1);
        }
    }
}
 类似资料: