Method One: Sort according to the JAVA1.8LAMBDA expression
comparator<rateinfo> Comparator = (T1, t2), T1.getratecode (). CompareTo (T2.getratecode ());
Method Two: Sort () by using the list method
List API:default voidsort(Comparator<? super E> c)
In fact, according to Comarator, this class
Rateinfolist.sort (comparator.reversed ());
Method Three: Sort by using the sort of the collections class
static <T> voidsort(List<T> list, Comparator<? super T> c)
Sorts the specified list according to the order induced by the specified comparator. Google Translate:sorts the specified list according to the order that the specified comparer is raised. In fact, this sort is also used
Comparatorclass to sort
ComparatorClass API: English translation can be
intCompare (t O1, T O2) compares its-arguments fororder.Static<t,uextendscomparable<?SuperU>>Comparator<T> comparing (function<?SuperT?extendsU>keyextractor) Accepts a function that extracts a comparable sort key from a type T, and returns a Comparator<T>That's compares by that sort key.Static<T,U> comparator<t> comparing (function<?SuperT?extendsU> keyextractor, comparator<?SuperU>keycomparator) Accepts a function that extracts a sort key from a type T, and returns a Comparator<T>That's compares by that sort key using the specified Comparator.Static<T> comparator<t> comparingdouble (todoublefunction<?SuperT>keyextractor) Accepts a function that extracts aDoubleSort key from a type T, and returns a comparator<t>That's compares by that sort key.Static<T> comparator<t> comparingint (tointfunction<?SuperT>keyextractor) Accepts a function that extracts anintSort key from a type T, and returns a comparator<t>That's compares by that sort key.Static<T> comparator<t> Comparinglong (tolongfunction<?SuperT>keyextractor) Accepts a function that extracts aLongSort key from a type T, and returns a comparator<t>That's compares by that sort key.Booleanequals (Object obj) indicates whether some other Object is"Equal to" ThisComparator.Static<textendscomparable<?SuperT>>Comparator<T>Naturalorder () Returns a comparator that compares comparable objects in natural order.Static<T> comparator<t> Nullsfirst (comparator<?SuperT>comparator) Returns aNULL-friendly comparator that considersNULLTo is less than non-NULL.Static<T> comparator<t> nullslast (comparator<?SuperT>comparator) Returns aNULL-friendly comparator that considersNULLTo be greater than non-NULL.defaultComparator<t>reversed () Returns a comparator that imposes the reverse ordering of ThisComparator.Static<textendscomparable<?SuperT>>Comparator<T>Reverseorder () Returns a comparator that imposes the reverse of the natural ordering.defaultComparator<t> thencomparing (comparator<?SuperT>Other ) Returns a lexicographic-order comparator with another comparator.default<uextendscomparable<?SuperU>>Comparator<T> thencomparing (function<?SuperT?extendsU>keyextractor) Returns a lexicographic-order comparator with a function that extracts a comparable sort key.default<U> comparator<t> thencomparing (function<?SuperT?extendsU> keyextractor, comparator<?SuperU>keycomparator) Returns a lexicographic-order comparator with a function, extracts a key to being compared with the given comparator.defaultComparator<t> thencomparingdouble (todoublefunction<?SuperT>keyextractor) Returns a lexicographic-order comparator with a function that extracts aDoublesort key.defaultComparator<t> thencomparingint (tointfunction<?SuperT>keyextractor) Returns a lexicographic-order comparator with a function that extracts aintsort key.defaultComparator<t> Thencomparinglong (tolongfunction<?SuperT>keyextractor) Returns a lexicographic-order comparator with a function that extracts aLongSort key.
Code:
Collections.sort (Rateinfolist, comparator.comparing (Rateinfo::getratecode));
Method Four: Overriding the Compare method by using Comparator's Anonymous object class
Code:
Collections.sort (Rateinfolist,NewComparator<rateinfo>(){ /** int compare (Rateinfo R1, Rateinfo R2) returns an integer of the base type, * Returns a negative number indicating: R1 is less than R2, * Returns 0 means: R1 and R2 are equal, * return positive number means: R1 greater than R2*/ Public intCompare (Rateinfo R1, Rateinfo R2) {Integer rateCode1=Integer.parseint (R1.getratecode ()); Integer RateCode2=Integer.parseint (R2.getratecode ()); //Sort Ascending according to the age of Ratecode if(RateCode1 >RateCode2) { return1; } if(RateCode1 = =RateCode2) { return0; } return-1; } });
The problem I encountered when I wrote the code, according to my understanding and the summary of the online information
Java collection stores objects and sorts them based on object properties