Java two ways to implement custom sorting for ArrayList

  • 2020-12-20 03:35:52
  • OfStack

This article illustrates two ways Java can implement custom collations for ArrayList. To share for your reference, the details are as follows:

Custom collations of list are implemented in Java in two main ways

1) Make the class of the object to be sorted implement the Comparable interface, override the compareTo(T o) method, define the collation rules in it, then you can directly call Collections.sort () to sort the object array


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

The test class:


import java.util.*;
public class Test {
  public static void printData(List<Student> list) {
    for (Student student : list) {
      System.out.println(" Student id :" + student.getId() + "  The name :" + student.getName() + "  age " + student.getAge() + "  height :" + 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);
  }
}

Results:


before sorted
 Student id :1  The name :A  age 20  height :180
 Student id :2  The name :B  age 21  height :175
 Student id :3  The name :C  age 22  height :190
 Student id :4  The name :D  age 21  height :170
 Student id :5  The name :E  age 20  height :185
after age and height sorted
 Student id :1  The name :A  age 20  height :180
 Student id :5  The name :E  age 20  height :185
 Student id :4  The name :D  age 21  height :170
 Student id :2  The name :B  age 21  height :175
 Student id :3  The name :C  age 22  height :190

2) Implement the comparator interface Comparator, rewrite the compare method, and directly pass it into sort as a parameter


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

The test class:


import java.util.*;
public class Test {
  public static void printData(List<Student> list) {
    for (Student student : list) {
      System.out.println(" Student id :" + student.getId() + "  The name :" + student.getName() + "  age " + student.getAge() + "  height :" + 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);
  }
}

Output results:


before sorted
 Student id :1  The name :A  age 20  height :180
 Student id :2  The name :B  age 21  height :175
 Student id :3  The name :C  age 22  height :190
 Student id :4  The name :D  age 21  height :170
 Student id :5  The name :E  age 20  height :185
after age sorted
 Student id :1  The name :A  age 20  height :180
 Student id :5  The name :E  age 20  height :185
 Student id :2  The name :B  age 21  height :175
 Student id :4  The name :D  age 21  height :170
 Student id :3  The name :C  age 22  height :190
after age and height sorted
 Student id :1  The name :A  age 20  height :180
 Student id :5  The name :E  age 20  height :185
 Student id :4  The name :D  age 21  height :170
 Student id :2  The name :B  age 21  height :175
 Student id :3  The name :C  age 22  height :190

Just from the above example you can see that the sorting is stable, so look at java's Collections.sort The source code, indeed, is based on a stable merge sort implementation, internally optimized, called TimSort. (refer to https about TimSort: / / baike baidu. com item/TimSort & # 63; fr = aladdin)

PS: Here is another demo tool on sorting for your reference:

Online animation demonstration of insert/Select/Bubble/merge/Hill/Quicksort algorithm process tools:
http://tools.ofstack.com/aideddesign/paixu_ys

For more information about java algorithm, please refer to Java Data Structure and Algorithm Tutorial, Java DOM Node Operation Skills Summary, Java File and Directory Operation Skills Summary and Java Cache operation Skills Summary.

I hope this article has been helpful in java programming.


Related articles: