Java implements two methods for customizing ArrayList sorting. javaarraylist
This example describes two Java implementation methods for ArrayList custom sorting. We will share this with you for your reference. The details are as follows:
In Java, the list can be sorted in two ways.
1) Let the class of the object to be sorted implement the Comparable interface, rewrite the compareTo (T o) method, and define the sorting rule in it, then you can directly call Collections. sort () to sort object arrays
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 ID:" + 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 ID: 1 Name: A age 20 Height: 180 student ID: 2 Name: B age 21 Height: 175 student ID: 3 name: C Age 22 Height: 190 student ID: 4 Name: D age 21 height: 170 student ID: 5 Name: E age 20 height: 185 after age and height sorted student ID: 1 Name: A age 20 height: 180 student ID: 5 Name: E age 20 Height: 185 student ID: 4 name: D age 21 Height: 170 student ID: 2 Name: B age 21 Height: 175 student ID: 3 name: C Age 22 Height: 190
2) Implement the Comparator interface Comparator, rewrite the compare method, and directly upload the interface as a parameter to 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; }}
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 () + "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 ID: 1 Name: A age 20 Height: 180 student ID: 2 Name: B age 21 Height: 175 student ID: 3 name: C Age 22 Height: 190 student ID: 4 Name: D age 21 Height: 170 student ID: 5 Name: E age 20 Height: 185 after age sorted student ID: 1 Name: A age 20 Height: 180 student ID: 5 Name: E age 20 Height: 185 student ID: 2 Name: B age 21 Height: 175 student ID: 4 name: D age 21 Height: 170 student ID: 3 name: C Age 22 height: 190 after age and height sorted student ID: 1 Name: A age 20 height: 180 student ID: 5 Name: E age 20 height: 185 student ID: 4 name: D age 21 height: 170 student ID: 2 Name: B age 21 Height: 175 student ID: 3 name: C Age 22 Height: 190
From the above example, we can see that the sorting is stable. Let's look at the javaCollections.sort
The source code is indeed implemented based on stable Merge Sorting, and is also optimized internally, called TimSort. (About TimSort can also refer to https://baike.baidu.com/item/TimSort? Fr = aladdin)
PS: here we recommend a demo tool for sorting for your reference:
Online animation demo insert/select/bubble/merge/hill/Quick Sort Algorithm process tool:
Http://tools.jb51.net/aideddesign/paixu_ys