-
-
Sort needs to be rewritten on its own for different objects.
ClassAImplementsComparable {Public IntCompareto (Object O) {A = (a) O; comparison method.> return value is greater than 0. <return value is less than 0. If the return value is equal, 0 is returned.
}}
-
-
In the actual call process:
-
-
-
List L =NewVertex list (); L. Add ([Object[1] of a]);... L. Add ([Object[2] of a]); collections. Sort (l); system. Out. println (L );
-
For more information about the following, see. Self-collected API documentation ..
-
-
-
Public interfaceComparable <t>
This interface forcibly sorts the objects of each class. This sort is calledNatural sorting, ClassComparetoMethod is called itsNatural Comparison Method.
You can useCollections. Sort(AndArrays. Sort. Objects that implement this interface can be used as keys in an ordered ing table or elements in an ordered set without specifying a comparator.
ForCEveryE1AndE2For example(E1.compareto (object) E2) = 0)AndE1.equals (object) E2)Class with the same Boolean ValueCThe natural sorting of is calledSame as equals. Note,NullIs not an instance of any class, even ifE. Equals (null)ReturnFalse,E. compareto (null)Will also throwNullpointerexception.
It is strongly recommended (though not required) to make the natural order consistent with equals. This is because when you use an element (or key) whose natural sorting is inconsistent with equals, the ordered set (and the ordered ing table) behavior without an explicit comparator is "weird ". In particular, such an ordered set (or an ordered ing table) violatesEqualsCommon conventions of the Set (or ing table) defined by the method.
For exampleAAndBAdd to an ordered set that does not use an explicit comparator, so that(! A. Equals (object) B) & A. compareto (object) B) = 0), Then the secondAddThe operation returns false (the size of the sorted set does not increase), because from the perspective of the sorted set,AAndBIs equivalent.
In fact, all Java core classes that are executed and compared have the same natural sorting as equals.Java. Math. bigdecimalIs an exception. Its natural sorting places values equal but with different precision.BigdecimalObjects (such as 4.0 and 4.00) are equivalent.
In order to tilt toward mathematics, a natural sorting is defined based on a given class C.LinkAs follows:
{(X, y) such that X. compareto (object) y) <= 0 }.
Overall sortingQuotientYes:
{(X, y) such that X. compareto (object) y) = 0 }.
It directly followsComparetoAgreement, operator isCOfEquivalence relationship, The natural sorting isCOfOverall sorting. When we talk about the natural sorting of classesSame as equalsWhen, it means that the vendor of the natural sorting is composed of ClassesEquals (object)The equivalence relationship defined by the method.
{(X, y) such that X. Equals (object) y )}.
Compareto
IntCompareto(T o)
-
-
Compare the order of the object with the specified object. If the object is smaller than, equal to, or greater than the specified object, a negative integer, zero, or positive integer is returned.
in the previous description, the symbol SGN ( Expression ) indicates the mathematical SIGNUM function, which is based on whether the value of Expression is negative, zero, or positive, -1 , 0 , and 1 are returned respectively. The implementation class must ensure that all x and Y have SGN (X. compareto (y) =-SGN (Y. compareto (x) . (This means that if Y. compareto (x) throws an exception, then X. compareto (y) also throws an exception .)
the implementation class must also ensure that the relationship is passed: (X. compareto (y)> 0 & Y. compareto (z)> 0) means X. compareto (z)> 0 .
Finally, the Program must be X. compareto (y) = 0 means that SGN (X. compareto (z) = SGN (Y. compareto (z) .
strongly recommended (X. compareto (y) = 0) = (X. equals (y) , but not strictly requires this. In general, this fact should be clearly pointed out for any comparable interface or class that violates this condition. We recommend that you elaborate: "NOTE: This class has a natural sorting that is inconsistent with equals ."
-
-
-
Parameters:
-
O-Object to be compared.
-
Return Value:
-
Negative integer, zero, or positive integer, based on whether the object is smaller than, equal to, or greater than the specified object.
-
Throw:
-
Classcastexception-If the specified object type cannot be compared with this object.
//////////////////////////////////////// ////////////////////////////////
Import Java. util .*; Public Class Test { Public Static Void Main (string ARGs []) {list L1 = New Vertex list (); l1.add ( New Name (" Karl "," M "); L1.add ( New Name (" Steven "," Lee "); L1.add ( New Name (" John "," O "); L1.add ( New Name (" Tom "," M "); System. Out. println (L1); collections. Sort (L1); system. Out. println (L1 );}} Class Name Implements Comparable { Private String firstname, lastname; Public Name (string firstname, string lastname ){ This . Firstname = firstname; This . Lastname = lastname ;} Public String getfirstname (){ Return Firstname ;} Public String getlastname (){ Return Lastname ;} Public String tostring (){ Return Firstname +" "+ Lastname ;} Public Boolean Equals (Object OBJ ){ If (OBJ Instanceof Name) {name = (name) OBJ; Return (Firstname. Equals (name. firstname) & lastname. Equals (name. lastname ));} Return Super . Equals (OBJ );} Public Int Hashcode (){ Return Firstname. hashcode ();} Public Int Compareto (Object O) {name n = (name) O; Int Lastcmp = lastname. compareto (N. lastname ); Return (Lastcmp! = 0? Lastcmp: firstname. compareto (N. firstname ));}}