java 自定义排序

实现Comparable接口

返回值
-1:第一个值和第二个值不交换位置
0:相等不变
1:交换位置

public class Student implements Comparable<Student> {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student s) {
        if (age < s.age) {
            return -1;
        } else if (age > s.age) {
            return 1;
        } else {
            return 0;
        }
    }

    public static void main(String[] args) {
        Student[] s = {new Student("s1", 99),
                new Student("s2", 19),
                new Student("s3", 89),
                new Student("s4", 29),
                new Student("s5", 39),
                new Student("s6", 9)};
        System.out.println("排序前:");
        for (Student student : s) {
            System.out.println(student);
        }
        Arrays.sort(s);
        System.out.println("排序后:");
        for (Student student : s) {
            System.out.println(student);
        }
    }
}

结果

排序前:
Student{name='s1', age=99}
Student{name='s2', age=19}
Student{name='s3', age=89}
Student{name='s4', age=29}
Student{name='s5', age=39}
Student{name='s6', age=9}
排序后:
Student{name='s6', age=9}
Student{name='s2', age=19}
Student{name='s4', age=29}
Student{name='s5', age=39}
Student{name='s3', age=89}
Student{name='s1', age=99}

结论:
age<s.age,返回-1,升序,可以这么记忆,age-s.age<0和-1一样是负数,负负得正,就为升序。
age<s.age,返回1,降序,age-s.age<0和-1相反,正负得负,降序

实现Comparator接口

先按年龄升序,相等在按名字长度升序,又相等在名字ASCII码升序(字典排序)

public class Student {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }


    public static void main(String[] args) {
        Student[] s = {new Student("s3", 99),
                new Student("s2", 19),
                new Student("s81", 89),
                new Student("s9", 89),
                new Student("s12", 29),
                new Student("s5", 39),
                new Student("s0", 39),
                new Student("s1", 9)

        };
        System.out.println("排序前:");
        for (Student student : s) {
            System.out.println(student);
        }
        Arrays.sort(s, new StudentComparator());
        System.out.println("排序后:");
        for (Student student : s) {
            System.out.println(student);
        }
    }
}
public class StudentComparator implements Comparator<Student> {

    @Override
    public int compare(Student s1, Student s2) {
        //先按照年龄升序
        if (s1.getAge() < s2.getAge()) {
            return -1;
        } else if (s1.getAge() > s2.getAge()) {
            return 1;
        } else {
            //如果年龄相等,则按照名字排序
            int difference = s1.getName().compareTo(s2.getName());
            int l1 = s1.getName().length();
            int l2 = s2.getName().length();
            //先按长度升序
            if (l1 < l2) {
                return -1;
            } else if (l1 > l2) {
                return 1;
            } else {
                //如果名字长度相等,且s1的ASCII码比s2的小,则升序(位置不变)
                if (difference < 0) {
                    return -1;
                } else if (difference > 0) {
                    return 1;
                } else {
                    return 0;
                }
            }
        }
    }
}
排序前:
Student{name='s3', age=99}
Student{name='s2', age=19}
Student{name='s81', age=89}
Student{name='s9', age=89}
Student{name='s12', age=29}
Student{name='s5', age=39}
Student{name='s0', age=39}
Student{name='s1', age=9}
排序后:
Student{name='s1', age=9}
Student{name='s2', age=19}
Student{name='s12', age=29}
Student{name='s0', age=39}
Student{name='s5', age=39}
Student{name='s9', age=89}
Student{name='s81', age=89}
Student{name='s3', age=99}

以上案例都是基于数组的,当然集合也可以调用Collections.sort(List<T> list,Comparator<? super T> c) 进行排序

Comparator 和 Comparable 比较

Comparable是排序接口;若一个类实现了Comparable接口,就意味着“该类支持排序”。
而Comparator是比较器;我们若需要控制某个类的次序,可以建立一个“该类的比较器”来进行排序。
我们不难发现:Comparable相当于“内部比较器”,而Comparator相当于“外部比较器”。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值