A simplest decorative pattern

xiaoxiao2021-03-06  112

If you want to sort the elements in a list, if the element in the list is sorted (implemented), direct collections.sort (List). If you want to specify the sort algorithm, you can use the Compare, you can refer: http: // Blog.9cbs.net/treeroot/archive/2004/10/19/142636.aspxcollections.sort is sorted in ascending order, if you want to descend the order? It is actually very simple, as long as you redefine a Comparator. List list = New arraylist (); list.add (new integer (3)); list.add (New Integer (53)); list.add (new integer (34)); collections.sort (list); // This is ascending Collections.Sort (List, Collections.ReverseOrder ()); // This is the above descending element for can be sorted, for unsatisfactory elements? List list = new arraylist (); list.add (new object (new object (new object (); list.add (new object ()); collections.sort (List, new Compare () {public int compare (Object O1, Object O2) {Return (O1.hashcode ) -O2.hashcode ());}) // This is ascending collections.sort (List, new compare () {public int compare (Object O1, Object O2) {return (O2.hashcode () - O1.hashcode )));}) // This is descending

No reason is so complicated, if it is more troublesome when compared (may need more than a few attributes), do you write this? Here we first see how Collection.ReverseOrder is implemented: private static final comparator reverse_order = new reverseComparator (); See the definition of this internal class: private static class ReverseComparator implements Comparator, Serializable {private static final long serialVersionUID = 7207038068494060240L; public int compare (Object o1, Object o2) {Comparable c1 = (Comparable) o1; Comparable c2 = (Comparable) O2;

转载请注明原文地址:https://www.9cbs.com/read-104052.html

New Post(0)