For custom objects, sort does not know the rules, so it cannot be compared. In this case, you must define the collation. There are two types of methods:
Java.lang there is an interface below: comparable (comparable)
You can have a custom object implement an interface that has only one method Comparableto (object o)
The rule is that the current object is compared to an O object, which returns an int value that the system sorts according to this value.
return value >0; (can define the return value as 1), as the current object >o object
If the current object =o object, the return value = 0;
Returns the value 〈0, such as the current object <o object. (The return value can be defined as-1)
Look at the Java code of Testarraylist.
We achieve ascending and descending permutations by returning values of 1 and 1 positions.
1 Packagetomtexts;2 3 4 Abstract classFatherclass5 {6 Abstract voidAbstractmethod ();7 voidPrintmethod ()8 {9System.out.println ("Fatherclass function!"); Ten } One } A classChildClassextendsFatherclass - { - voidAbstractmethod () the { -System.out.println ("ChildClass function!"); - } - } + Public classtomtexts_07 - { + Public Static voidMain (String args[]) A { atChildClass c=NewChildClass (); - C.abstractmethod (); - C.printmethod (); - - } -}
Java.lang there is an interface below: comparable (comparable)