Here is the knowledge about the sort of Java collections
One:
Import Java.util.arraylist;import Java.util.collections;import Java.util.list;public class SortListDSemo2 {public static void Main (string[] args) { list<point> list=new arraylist<point> (); List.add (new Point (4,5)); List.add (new Point (2,4)); List.add (new Point (4,6)); List.add (new Point (9,6)); SYSTEM.OUT.PRINTLN (list); / * * Collections The Sor method requires that the sorted collection element must implement the comparable interface * That interface is used to represent a method that can be compared to provide an abstraction Comparato used to define a comparison rule. * * /Collections.sort (list); SYSTEM.OUT.PRINTLN (list);}}
Two:
Import Java.util.arraylist;import java.util.collection;import java.util.collections;import java.util.Comparator; Import java.util.List; /** * Collections's Sort method has two disadvantages: * 1: Requires that our collection element must implement the comparable interface * This is a strong "intrusive" for our class; * 2: Sometimes the collection element implements the Comparale interface and defines the rule of comparison size * But the rules are not in the group we need for sorting * Collections provides an overloaded sort method: * static void sort (List list,comparable c) * This overloaded method requires us to pass in an extra comparator * according to the comparator's rules Compare elements in a collection natural sort * @author soft01 * */public class Demo1 {public static void main (string[] args) {list<string> List=ne W arraylist<string> (); List.add ("Xiaoming 16:19 05 sec"); List.add ("Dawn"); List.add ("No light in front of the window"); SYSTEM.OUT.PRINTLN (list); Mycomparator com=new mycomparator (); Collections.sort (list,com); SYSTEM.OUT.PRINTLN (list);} } Class Mycomparator implements comparator<string>{@Overridepublic int Compare (string O1, String O2) {return O1.leng Th ()-o2.length ();} }
Java Collections's Sort API