Java's sort method is generally two, divided Array.Sort () and Collection.sort ()
Arrays.sort (t[], Comparator < super T > C) method is used to sort the object array by user custom rule. Collections.sort (LIST<T>, Comparator <? super T > C) methods are used for object collections sorted by user-defined rules. Comparable is a generic interface that users can implement to accomplish their own specific comparisons. We use arrays to sort the arrays, using collections to sort the combined frame containers, such as arrayslist,linkedlist. Generally when the object itself implements the comparable interface, such as the string type and the integer type, orthe lists object string itself implements the comparable interface and contains the CompareTo method, so you can call the sort method directly, sort by natural order, ascending sort. However, when the object does not implement the interface, it is sorted by a custom method, such ascollections.sort (list, new Pricecomparator ()); This requires implementation of the interface, write CompareTo method://Custom Comparator: Sort by book price static class Pricecomparator implements Comparator { public int Compare (object Object1, Object object2) {//method implemented in interface
Book P1 = (book) Object1; //Forced conversion
Book P2 = (book) Object2;
Return new Double (P1.price). CompareTo (new Double (P2.price));
}
}
The sort method of Java