Java list sorting, javalist
1. Introduction
This is different from the sorting of arrays.
In fact, Java has implemented sorting for arrays and lists.ArrayYou can directly useArrays. sort,For List and VectorFor example, youYou can use the Collections. sort method..
Java API provides two methods for sorting set types:
java.util.Collections.sort(java.util.List)java.util.Collections.sort(java.util.List, java.util.Comparator)
If the elements in the set are of the same type and the Comparable interface is implemented, you can directly call the first method.
If you have other sorting ideas, such as you don't want to sort by nature, you can also pass a Comparator, such as reverse.
Different elements are complex.
2. Sort the list by implementing the Comparable Interface
If we have a list set of Person classes and want them to be sorted by an Order attribute, we can let the Person class implement the Comparable interface and rewrite its CompareTo method. The specific implementation is as follows:
1) Person entity class
Public class Person implements Comparable <Person> {private String name; private Integer order;/*** @ return the name */public String getName () {return name ;} /*** @ param name * the name to set */public void setName (String name) {this. name = name;}/*** @ return the order */public Integer getOrder () {return order ;} /*** @ param order * the order to set */public void setOrder (Integer order) {this. order = order ;}
@ Override public int compareTo (Person arg0) {return this. getOrder (). compareTo (arg0.getOrder (); // define your sorting rule here. }}
By Rewriting the compareTo method of the Comparable interface, the program can be sorted according to the arrangement we want. For example, here I want the Person to be sorted in ascending order according to the order attribute.
2) test class
Public static void main (String [] args) {// initialize the data List <Person> listA = new ArrayList <Person> (); Person p1 = new Person (); person p2 = new Person (); Person p3 = new Person (); p1.setName ("name1"); p1.setOrder (1); p2.setName ("name2"); p2.setOrder (2 ); p3.setName ("name3"); p3.setOrder (3); listA. add (p2); listA. add (p1); listA. add (p3); // sort Collections. sort (listA); // print the sorted Person for (Person p: listA) {System. out. println (p. getName ());}}
3) Result:
name1name2name3
3. Reload the Collections. sort method.
Directly reload the java. util. Collections. sort (java. util. List, java. util. Comparator) method. You can flexibly modify the sorting method as follows:
1) Person entity class
It is the same as the above class, but the Comparable interface is not implemented
public class Person { private String name; private Integer order; /** * @return the name */ public String getName() { return name; } /** * @param name * the name to set */ public void setName(String name) { this.name = name; } /** * @return the order */ public Integer getOrder() { return order; } /** * @param order * the order to set */ public void setOrder(Integer order) { this.order = order; } }
2) test class
Public static void main (String [] args) {List <Person> listA = new ArrayList <Person> (); Person p1 = new Person (); person p2 = new Person (); Person p3 = new Person (); p1.setName ("name1"); p1.setOrder (1); p2.setName ("name2"); p2.setOrder (2 ); p3.setName ("name3"); p3.setOrder (3); listA. add (p2); listA. add (p1); listA. add (p3); // add our sorting rule Collections directly here. sort (listA, new Comparator <Person> () {public int compare (Person arg0, Person arg1) {return arg0.getOrder (). compareTo (arg1.getOrder () ;}}); for (Person p: listA) {System. out. println (p. getName ());}}
As shown above, a Comparator interface is directly rewritten in the Conllections. sort () method. Different sorting methods of the Person set can be used in different places. For example, if the order attribute of Person is listed in ascending order, I only need to rewrite this method to sort the list set in other places according to other rules, but this code looks more complicated than the previous method.
3) Result
name1name2name3
Thank you: Thank you for reading this article!