I am calling Collections.sort() on an ArrayList using a Comparator that I declared earlier.
ArrayList<Employee> list = new ArrayList<Employee>();
Comparator<Employee> comparator = new Comparator<Employee>() {
public int compare(Employee o1, Employee o2) {
return o1.getName().toLowerCase().compareTo(o2.getName().toLowerCase());
}
};
...
Collections.sort(list, comparator);
For some reason, sort is trying to cast the elements of my ArrayList as Comparables, even though I passed a Comparator. Why might this be happening?
If it's of any use, here is my stacktrace
Exception in thread "Thread-3" java.lang.ClassCastException: processing.app.EmployeeManager$PrettyOkayEmpolyee cannot be cast to java.lang.Comparable
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at foobar.Main.doSomeSorting(Main.java:140)
...
at java.lang.Thread.run(Unknown Source)