I need to Implement a static method in my class Student
public static Comparator<Student> getCompByName()
that returns a new comparator object for Student that compares 2 Students objects by the attribute 'name'.
Iv created what I think is correct, but I am not sure if it will function like it is supposed to from the requirements given above. I am very new with comparators and having a hard time with them.
public static Comparator<Student> getCompByName()
{
Comparator comp = new Comparator<Student>(){
@Override
public int compare(Student s1, Student s2)
{
return Integer.compare(s1.name.length(), s2.name.length());
}
};
return comp;
}
Just from reading the requirements stated above, I am not sure what I am supposed to be comparing about the 'name', whether it be the length, if they are equal, etc.. So I threw in an Integer compare. Is it clear to anyone from what is required and if I have done this correctly? If not, can anyone let me know what I need to do/change?