1. 程式人生 > >Java ArrayList的自定義排序

Java ArrayList的自定義排序

Java中實現對list的自定義排序主要通過兩種方式

1)讓需要進行排序的物件的類實現Comparable介面,重寫compareTo(T o)方法,在其中定義排序規則,那麼就可以直接呼叫Collections.sort()來排序物件陣列

public class Student implements Comparable{
    private int id;
    private int age;
    private int height;
    private String name;

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

    public int getId() {
        return id;
    }

    public int getAge() {
        return age;
    }

    public int getHeight() {
        return height;
    }

    public String getName() {
        return name;
    }

    public void setId(int id) {
        this.id = id;
    }

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

    public void setName(String name) {
        this.name = name;
    }

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

    @Override
    public int compareTo(Object o) {
        Student s = (Student) o;
        if (this.age > s.age) {
            return 1;
        }
        else if (this.age < s.age) {
            return -1;
        }
        else {
            if (this.height >= s.height) {
                return 1;
            }
            else {
                return -1;
            }
        }
    }
}

測試類:

import java.util.*;

public class Test {

    public static void printData(List<Student> list) {
        for (Student student : list) {
            System.out.println("學號:" + student.getId() + " 姓名:" + student.getName() + " 年齡" + student.getAge() + " 身高:" + student.getHeight());
        }
    }

    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student(1, "A", 20, 180));
        list.add(new Student(2, "B", 21, 175));
        list.add(new Student(3, "C", 22, 190));
        list.add(new Student(4, "D", 21, 170));
        list.add(new Student(5, "E", 20, 185));
        System.out.println("before sorted");
        printData(list);
        Collections.sort(list);
        System.out.println("after age and height sorted");
        printData(list);
    }
}

結果:

before sorted
學號:1 姓名:A 年齡20 身高:180
學號:2 姓名:B 年齡21 身高:175
學號:3 姓名:C 年齡22 身高:190
學號:4 姓名:D 年齡21 身高:170
學號:5 姓名:E 年齡20 身高:185
after age and height sorted
學號:1 姓名:A 年齡20 身高:180
學號:5 姓名:E 年齡20 身高:185
學號:4 姓名:D 年齡21 身高:170
學號:2 姓名:B 年齡21 身高:175
學號:3 姓名:C 年齡22 身高:190

2)實現比較器介面Comparator,重寫compare方法,直接當做引數傳進sort中

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

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

    public int getId() {
        return id;
    }

    public int getAge() {
        return age;
    }

    public int getHeight() {
        return height;
    }

    public String getName() {
        return name;
    }

    public void setId(int id) {
        this.id = id;
    }

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

    public void setName(String name) {
        this.name = name;
    }

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

測試類:

import java.util.*;

public class Test {

    public static void printData(List<Student> list) {
        for (Student student : list) {
            System.out.println("學號:" + student.getId() + " 姓名:" + student.getName() + " 年齡" + student.getAge() + " 身高:" + student.getHeight());
        }
    }

    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student(1, "A", 20, 180));
        list.add(new Student(2, "B", 21, 175));
        list.add(new Student(3, "C", 22, 190));
        list.add(new Student(4, "D", 21, 170));
        list.add(new Student(5, "E", 20, 185));
        System.out.println("before sorted");
        printData(list);
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if(o1.getAge() >= o2.getAge()) {
                    return 1;
                }
                else {
                    return -1;
                }
            }
        });
        System.out.println("after age sorted");
        printData(list);
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if(o1.getAge() > o2.getAge()) {
                    return 1;
                }
                else if (o1.getAge() < o2.getAge()){
                    return -1;
                }
                else {
                    if (o1.getHeight() >= o2.getHeight()) {
                        return 1;
                    }
                    else {
                        return -1;
                    }
                }
            }
        });
        System.out.println("after age and height sorted");
        printData(list);
    }
}

輸出結果:

before sorted
學號:1 姓名:A 年齡20 身高:180
學號:2 姓名:B 年齡21 身高:175
學號:3 姓名:C 年齡22 身高:190
學號:4 姓名:D 年齡21 身高:170
學號:5 姓名:E 年齡20 身高:185
after age sorted
學號:1 姓名:A 年齡20 身高:180
學號:5 姓名:E 年齡20 身高:185
學號:2 姓名:B 年齡21 身高:175
學號:4 姓名:D 年齡21 身高:170
學號:3 姓名:C 年齡22 身高:190
after age and height sorted
學號:1 姓名:A 年齡20 身高:180
學號:5 姓名:E 年齡20 身高:185
學號:4 姓名:D 年齡21 身高:170
學號:2 姓名:B 年齡21 身高:175
學號:3 姓名:C 年齡22 身高:190

單從上面的例子可以看出排序是穩定的,去看了下java的Collections.sort的原始碼,確實是基於穩定的歸併排序實現的,內部還做了優化,叫TimSort