java 集合排序、二次排序与Comparable 接口

说明

当一个集合中的元素,需要排序时,元素的类需要实现 Comparable 接口

接口方法

实现 Comparable 接口后,需要重写下面方法:

public int compareTo(T o);

形参(重点)

  • 当前对象 表示集合中的 下一个元素

  • 形参对象 表示集合中的 上一个元素(前一个元素)

返回值(重点)

方法返回值是int,有三种情况:

  • 正整数,位置不变
  • 负整数,此时 交换这2个元素的位置
  • 0,位置不变,表示 两个对象相等。(二次排序时,用到 0)

排序规则

排序规则-正序(从小到大)

  • 如果当前对象(下一个元素)大于 形参(前面的元素),返回 正整数,位置不变

  • 如果当前对象(下一个元素)小于 形参(前面的元素),返回 负整数,会交换位置,即:小的在前,如下图:

排序规则-倒序(从大到小)

  • 如果当前对象(下一个元素) 小于 形参(前面的元素),返回 正整数,位置不变

  • 如果当前对象(下一个元素) 大于 形参(前面的元素),返回 负整数,会交换位置,即:大的在前

案例

当 Student类对象,根据 年龄 从小到大排序时 (正序),就需要实现 Comparable 接口

Student类

package sorttest;

public class Student implements Comparable<Student> {

    private String name;
    private int age;
    private int length;

    public Student(String name, int age,int length) {
        this.name = name;
        this.age = age;
        this.length = length;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    public int getLength() {
        return length;
    }
    public void setLength(int length) {
        this.length = length;
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return name+","+age+","+length;
    }
    @Override
    public int compareTo(Student o) {
        int res=0;
        //当前对象的年龄 < 其他对象的年龄,返回-1,表示当前对象在前,即:年龄小的在前
        if( this.age < o.getAge()){
            res=-1;
        }else if(this.age > o.getAge()){
            res=1;
        }else{//相等
            res=0;
        }

        return res;
    }


}

测试类

package sorttest;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import sort.Cdz;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Student std=new Student("李雷",20,182);
        Student std2=new Student("lucy",16,168);
        Student std3=new Student("lili",16,167);
        Student std4=new Student("韩梅梅",25,165);
        Student std5=new Student("张三",16,185);


        List<Student> list=new ArrayList();
        list.add(std);
        list.add(std2);
        list.add(std3);
        list.add(std4);
        list.add(std5);
        Collections.sort(list);

        for(Student item:list){
            System.out.println(item);
        }
    }

}

执行结果:

lucy,16,168
lucy,16,167
张三,16,185
李雷,20,182
韩梅梅,25,165

二次排序

上面的执行结果中,当年龄相同时,根据 身高从大到小排序

修改 Student类

public class Student implements Comparable<Student> {

    private String name;
    private int age;
    private int length;

    public Student(String name, int age,int length) {
        this.name = name;
        this.age = age;
        this.length = length;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    public int getLength() {
        return length;
    }
    public void setLength(int length) {
        this.length = length;
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return name+","+age+","+length;
    }
    @Override
    public int compareTo(Student o) {
        int res=0;
        //当前对象的年龄 < 其他对象的年龄,返回-1,表示当前对象在前,即:年龄小的在前
        if( this.age < o.getAge()){
            res=-1;
        }else if(this.age > o.getAge()){
            res=1;
        }else{//相等
//            res=0;
            //二次排序
            //当前对象较大,返回-1,表示较大的在前,即:倒序
            if(this.length>o.length){
                res=-1;
            }else if(this.length < o.length){
                res=1;
            }else{
                res=0;
            }
        }

        return res;
    }
}

原文出处:/show_1IX2DO38AerI.html