This article describes two implementation methods for Java custom sorting for ArrayList. Share it for your reference, as follows:
Implementing custom sorting of lists in Java mainly uses two ways
1) Let the class of the object that needs to be sorted implement the Comparable interface, override the compareTo(To) method, and define the sorting 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; } } }}Test class:
import java.util.*;public class Test { public static void printData(List<Student> list) { for (Student student : list) { System.out.println("Student number:" + student.getId() + " 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); }}result:
before sorted student number:1 Name: A Age 20 Height: 180 Student number: 2 Name: B Age 21 Height: 175 Student number: 3 Name: C Age 22 Height: 190 Student number: 4 Name: D Age 21 Height: 170 Student number: 5 Name: E Age 20 Height: 185 After age and height sorted student number: 1 Name: A Age 20 Height: 180 Student number: 5 Name: E Age 20 Height: 185 Student number: 4 Name: D Age 21 Height: 170 Student number: 2 Name: B Age 21 Height: 175 Student number: 3 Name: C Age 22 Height: 190
2) Implement the comparator interface Comparator, rewrite the compare method, and 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; }}Test class:
import java.util.*;public class Test { public static void printData(List<Student> list) { for (Student student : list) { System.out.println("Student number:" + student.getId() + " 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 result:
before sorted student number:1 Name: A Age 20 Height: 180 Student number: 2 Name: B Age 21 Height: 175 Student number: 3 Name: C Age 22 Height: 190 Student number: 4 Name: D Age 21 Height: 170 Student number: 5 Name: E Age 20 Height: 185 After age sorted student number: 1 Name: A Age 20 Height: 180 Student number: 5 Name: E Age 20 Height: 185 Student number: 2 Name: B Age 21 Height: 175 Student number: 4 Name: D Age 21 Height: 170 Student number: 3 Name: C Age 22 Height: 190 After age and height sorted student number: 1 Name: A Age 20 Height: 180 Student number: 5 Name: E Age 20 Height: 185 Student number: 4 Name: D Age 21 Height: 170 Student number: 2 Name: B Age 21 Height: 175 Student number: 3 Name: C Age 22 Height: 190
From the above examples, we can see that the sorting is stable. I looked at the source code of Java Collections.sort . It is indeed implemented based on stable merge sorting and sorting. It has also been optimized internally, called TimSort. (For TimSort, please refer to https://baike.baidu.com/item/TimSort?fr=aladdin)
PS: Here is a demonstration tool for your reference:
Online animation demonstration insert/select/bubble/merge/hill/quick sorting algorithm process tool:
http://tools.VeVB.COM/aideddesign/paixu_ys
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.