First: Comparable sort interface If a class implements the comparable interface, it means "this class supports sorting." Assuming "There is a list (or array) of elements that are classes that implement the comparable interface, the list (or array) can be sorted by Collections.sort (or Arrays.sort). In addition, the "object of the class that implements the comparable interface" can be used as a key in an ordered map (such as treemap) or an element in an ordered collection (TreeSet), without specifying a comparer.
/* Entity class */package Com.hou.test1;public class sorta implements comparable<sorta> {private String name; private Integer order; Public String GetName () {return name; } public void SetName (String name) {this.name = name; } public Integer GetOrder () {return order; public void Setorder (Integer order) {this.order = order; } @Override Public String toString () {return ' name: ' + name + '--order: ' + order; } @Overridepublic int CompareTo (sorta a) {//Return A.getorder ()-this.order;//Ascending//return This.order-a.getorder ();//reverse Order/ /return This.order.compareTo (A.getorder ());//Ascending return A.getorder (). CompareTo (This.order);//reverse}}/* test class */package Com.hou.test1;import Java.util.arraylist;import Java.util.collections;import Java.util.comparator;import Java.util.list;public class Sorttest {public static void main (string[] args) {//First Method Example: list<string> lists = New Arraylist<string> (); Lists.add ("5"); LiSts.add ("2"); Lists.add ("9"); The Lists object string itself contains the CompareTo method, so you can call the sort method directly, sorted in natural order, that is, ascending sort collections.sort (lists); Example of the first method: list<sorta> ListA = new arraylist<sorta> (); Sorta A1 = new Sorta (); A1.setname ("a"); A1.setorder (2); Sorta A2 = new Sorta (); A2.setname ("B"); A2.setorder (1); Sorta a3 = new Sorta (); A3.setname ("C"); A3.setorder (5); Lista.add (A1); Lista.add (A2); Lista.add (A3); The object A in list implements the comparable interface Collections.sort (ListA); SYSTEM.OUT.PRINTLN (lists); System.out.println (ListA);}} /* Output Results */[2, 5, 9][name:c--order:5, Name:a--order:2, name:b--order:1]
The second type: Comparator comparator interface. If we need to control the order of a class, and the class itself does not support ordering (that is, the comparable interface is not implemented), we can create a "comparator" to sort it. This "comparator" only needs to implement the comparator interface. Collections.sort (list, new Pricecomparator ()) parameter one: The list parameter needs to be sorted two: The comparator, the class that implements the comparator interface, returns the value of an int, which is the equivalent of a flag, Tells the sort method in what order to sort the list. Comparator is an interface that overrides the two methods of compare () and equals () to compare functions, or, if NULL, to use the default order of the elements, such as A,b,c,d,e,f,g, which is a,b,c,d,e,f,g, The same is true of the numbers. Compare (A, B) method: Returns a negative integer, 0, or a positive integer, respectively, based on the first parameter less than, equal to, or greater than the second parameter. Equals (obj) method: Returns True only if the specified object is also a Comparator and enforces the same sort as this Comparator.
/* Entity class */package Com.hou.test1;public class Sorta {private String name; private Integer order; Public String GetName () {return name; } public void SetName (String name) {this.name = name; } public Integer GetOrder () {return order; public void Setorder (Integer order) {this.order = order; } @Override Public String toString () {return ' name: ' + name + '--order: ' + order; }}/* Test class */package Com.hou.test1;import java.util.arraylist;import java.util.collections;import java.util.Comparator Import Java.util.list;public class Sorttest {public static void main (string[] args) {//First Method Example: list<string> Lists = new arraylist<string> (); Lists.add ("5"); Lists.add ("2"); Lists.add ("9"); The Lists object string itself contains the CompareTo method, so you can call the sort method directly, sorted in natural order, that is, ascending sort collections.sort (lists); Example of the first method: list<sorta> ListA = new arraylist<sorta> (); Sorta A1 = new SortA (); A1.setname ("a"); A1.setorder (2); Sorta A2 = new Sorta (); A2.setname ("B"); A2.setorder (1); Sorta a3 = new Sorta (); A3.setname ("C"); A3.setorder (5); Lista.add (A1); Lista.add (A2); Lista.add (A3); The object A in list implements the comparable interface//collections.sort (lists); Collections.sort (ListA, New comparator<sorta> () {public int compare (sorta s1, sorta s2) {/** * Ascending row is the first parameter. CompareTo (second parameter); * Descending row is the second parameter. CompareTo (first parameter); */Return S2.getorder (). CompareTo (S1.getorder ()); }}); SYSTEM.OUT.PRINTLN (lists); System.out.println (ListA);}} /* Results Output */[2, 5, 9][name:c--order:5, Name:a--order:2, name:b--order:1]
Collections.sort () Sorting in Java