Java針對ArrayList自定義排序的2種實現方法


Comparator接口可以實現自定義排序,實現Comparator接口時,要重寫compare方法: 
  int compare(Object o1, Object o2) 返回一個基本類型的整型 
  如果要按照升序排序,則o1 小於o2,返回-1(負數),相等返回0,01大於02返回1(正數) 
  如果要按照降序排序,則o1 小於o2,返回1(正數),相等返回0,01大於02返回-1(負數)

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

 

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM