@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 for comparing two Student
objects based on their name. Is it possible to compare two such objects based on multiple fields i.e., both name and age?
Yes it is possible to compare two objects based on different sort sequences using Comparator
interface compare
method.
You need to create a sort sequence class. Sorting user defined objects using comparator