In today's leetcode, there are two of them. The use of interfaces to implement the ordering of objects. RELATED links to the two questions:
1. Sorting objects using the comparable interface
2. Using comparator interface to achieve sorting
Because the two interfaces have not been touched before. It is not clear what they are for the moment, so we find a lot of information on the Internet. Now it's a little bit clearer. Now the record, what is missing, welcome to correct in time
1.comparable<t> interface
Describing this interface in the Java API documentation is to force the overall ordering of the objects that implement each of its classes-----called the natural ordering of the class, the list of objects that implement this interface and the array can be actively sorted by Collections.sort (), and Arrays.sort ().
That is, the object (array) that only implements the interface is equivalent to the ability to sort. So it is called comparable---sortable. So be able to say that this is an internal sort of way, by implementing it the only way CompareTo (), about this method will be mentioned below;
2.comparator<t> interface
For it, it is for some of the more capable objects (arrays) that do not have their own capabilities. So it's called the comparison device , which is an external thing, by defining the comparison method , and then uploading to Collection.sort () and Arrays.sort () to sort the target, And by its own method compare () define the comparative content and the ascending order of the results.
3. Questions about order result ascending and descending
When learning to use these two interfaces, see that every program that implements them is doing the same.
For CompareTo ():
public int compareTo (Object o) { return this.val-o.val; Assuming the comparison is the Val attribute}
In this way, the objects implementing the comparable interface can be sorted in ascending order;
And for the Compare () method, this is the case:
Ascending: public int compare (Object a,object b) { return a.val-b.val;} Descending: public int compare (Object a,object b) { return b.val-a.val;}
When the two methods are seen to be implemented. I feel a little confused for a moment, to see the API documentation, but there is only a mention on the basis of the size of each return 1. -1. 0. Why is it possible to have different results in ascending and descending order when this method is passed into the sort () function? because the size of the two objects passed in is inconsistent, so the returned value is not necessarily, so I think it will not be in accordance with the result of the return value of the positive or negative to infer ascending or descending , so the internet to see a lot of answers. We all describe the narrative is not very clear, all just to mention how to be able to ascending, how can be descending. But it doesn't explain why. How is that inferred?
Later I found a description of the sort () function in arrays, which was able to sort all or part of an array of elements in ascending order !!
. so the sort () function that is called after the two interfaces are implemented is sorted in ascending order. So The two methods above are for it to infer the size of the two elements, and sort () will arrange all the elements in ascending order ;
For example, CompareTo () is a sort implementation within an object, which is arranged according to the natural order of the object, and when the size of the two objects is inferred, the result of CompareTo () is tested. Greater than 0 feel this.val large, and vice versa. Similarly, when calling the Comparetor. Sort () passes the two parameters of the compare (a, b), and the return value defaults to greater than 0, which is the value of a whose property is greater than B, and vice versa; So assuming that we implement the CompareTo () and compare () methods in the order of the sort () to infer two objects, that is the result of ascending Instead of assuming that we are coming back with O.val-this.val or B.val-a.val, the value is the opposite of the correct two value, and sort () does not know in ascending order what it feels. Small in the front. Big in the back, then the actual situation will become large in front, small in the after, so that the result is in descending order ~ ~ ~
About the comparator interface and comparable interfaces and their respective methods compare () and CompareTo ()