Java Comparator compareToIgnoreCase
I am trying to sort an array of strings using compareToIgnoreCase.
The string contain names ex: Billy Jean
When I try to compare them I get a null pointer exception. I think this is
because of the space between the first and last name. How can I compare
whole name?
Thanks
class CompareStudentNames implements Comparator{
//Sorts the students by name ignoring case.
@Override
public int compare(Object o1, Object o2) {
String name1 = ((Student)o1).getName();
String name2 = ((Student)o2).getName();
return (name1.compareToIgnoreCase(name2));
}
}
No comments:
Post a Comment