+ Start a Discussion

Comparable Interface Not Working

I'm trying to sort a list of my custom Apex class objects. I always receive this error message: 
System.ListException: One or more of the items in this list is not Comparable

To help trouble shoot, I'm literally running this Salesforce example code in the Developer Console and still receiving this error message.

Was a bug introduced or is there something wrong with my Salesforce org?

List<Employee> empList = new List<Employee>();
empList.add(new Employee(101,'Joe Smith', '4155551212'));
empList.add(new Employee(101,'J. Smith', '4155551212'));
empList.add(new Employee(25,'Caragh Smith', '4155551000'));
empList.add(new Employee(105,'Mario Ruiz', '4155551099'));

// Sort using the custom compareTo() method

// Write list contents to the debug log

// Verify list sort order.
System.assertEquals('Caragh Smith', empList[0].Name);
System.assertEquals('Joe Smith', empList[1].Name); 
System.assertEquals('J. Smith', empList[2].Name);
System.assertEquals('Mario Ruiz', empList[3].Name);

public class Employee implements Comparable {

    public Long id;
    public String name;
    public String phone;
    // Constructor
    public Employee(Long i, String n, String p) {
        id = i;
        name = n;
        phone = p;
    // Implement the compareTo() method
    public Integer compareTo(Object compareTo) {
        Employee compareToEmp = (Employee)compareTo;
        if (id == compareToEmp.id) return 0;
        if (id > compareToEmp.id) return 1;
        return -1;        

Yes, I've seen that post.  I believe I'm doing exactly what it recommends by implement Comparable.  It still doesn't work.