directory
One, foreword
Second, the body
1. Plan 1
2. Plan 2
3. Plan three
4. Plan 4
Third, summary
Fourth, the end
One, foreword
Sometimes we need to sort a List. How do we do that in Java? This article will document several ways to sort a List in Java, and hopefully it will help you.
Second, the body
1. Plan 1
1.1. Simple sorting methods, such as the following code
import java.util.ArrayList; import java.util.Collections; import java.util.List; Public class sort {public static void main(String[] args) {List<Integer> List = new ArrayList<Integer>(); List. The add (new Integer (3)); List. The add (new Integer (14)); List. The add (new Integer (6)); list.add(new Integer(10)); Collections.sort(list); // Sort collections.sort (list); System. The out. Println (list. The toString ()); }}Copy the code
The output is:
,6,10,14 [3]Copy the code
This simple sorting will output the natural data directly.
2. Plan 2
2.1. Relatively complex sorting method of parameters, in which parameters are objects
import java.util.ArrayList; import java.util.Collections; import java.util.List; Public class sort {public static void main(String[] args) {List<User> List = new ArrayList<User>(); List. add(new User(" 三", 5)); list.add(new User(" 三", 5)); List. add(new User(" li3 ", 30)); List. add(new User(" wang5 ", 19)); List. add(new User(" wang Zizi ", 17)); // Sort collections.sort (list); System. The out. Println (list. The toString ()); }} class User implements Comparable<User>{private String name; // private int age; Public User(String name, int age) {this.name = name; this.age = age; } // getter &&setter public String getName() {return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @override public String toString() {return "User [name=" + name + ", age=" + age + "]"; } @override public int compareTo(User User) {// Override compareTo(User User) { Return this.age-user-getage (); return this.age-user-getage (); }}Copy the code
Program running results: sorted by age ascending
[User [name= Zhang SAN, age=5], User [name= Wang Mazi, age=17], User [name= Wang Wu, age=19], User [name= Li Si, age=30]]Copy the code
3. Plan three
3.1. Use anonymous inner classes for sorting
import java.util.ArrayList; import java.util.Collections; import java.util.List; Public class sort {public static void main(String[] args) {List<User> List = new ArrayList<User>(); List. add(new User(" 三", 5)); list.add(new User(" 三", 5)); List. add(new User(" li3 ", 30)); List. add(new User(" wang5 ", 19)); List. add(new User(" wang Zizi ", 17)); Collections.sort(list, new Comparator<User>() {@override public int compare(User u1, User u2) {int diff = u1.getage () -u2.getage (); If (diff > 0) {return 1; }else if (diff < 0) {return -1; } return 0; // equal to 0}}); System.out.println(list.tostring ()); // Sort by age system.out.println (list.tostring ()); }}Copy the code
The running results are as follows:
[User [name= Zhang SAN, age=5], User [name= Wang Mazi, age=17], User [name= Wang Wu, age=19], User [name= Li Si, age=30]]Copy the code
4. Plan 4
4.1, is also the most brief scheme, the blogger recommended to use, a line of code can be done
import java.util.Collections; import java.util.List; Public class sort {public static void main(String[] args) {List<User> List = new ArrayList<User>(); List. add(new User(" 三", 5)); list.add(new User(" 三", 5)); List. add(new User(" li3 ", 30)); List. add(new User(" wang5 ", 19)); List. add(new User(" wang Zizi ", 17)); List.sort (Comparator.comparing(User::getAge)); System. The out. Println (list. The toString ()); }}Copy the code
The output is:
[User [name= Zhang SAN, age=5], User [name= Wang Mazi, age=17], User [name= Wang Wu, age=19], User [name= Li Si, age=30]]Copy the code
Third, summary
The first two methods are to implement the Comparable interface with an entity and rewrite the compareTo method. The first method is not to implement the Interger interface and implement the compareTo method. Java already implemented it for us, so we don’t have to write it again
The third way is to implement the compare method through an anonymous inner class implementing the Comparator interface and sorting through a custom Comparator.
Fourth, the end
If you find this article helpful, click “like” and save it for yourself. You may not find it next time you use it.