Java – implement custom CompareTo
•
Java
@Override
@Override public int compareTo(Object t) { if(t instanceof Student) { Student s = (Student)t; return (this.name.compareTo(s.name)); } else return -1; }
This is my CompareTo method implementation, which is used to compare two student objects by name Can two such objects, name and age, be compared based on multiple fields?
Solution
Yes, you can use the comparator interface comparison method to compare two objects based on different sort order
You need to create a sort sequence class Sorting user defined objects using comparator
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码