Java – implement custom CompareTo

@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
分享
二维码
< <上一篇
下一篇>>