エラー Exception in thread "main" java.lang.ClassCastException : com.genous.Employee cannot be cast to java.lang.Comparable が発生します
java.util.Arrays.mergeSort (Arrays.java:1157) で
java.util.Arrays.sort(Arrays.java:1092) で
java.util.Collections.sort(Collections.java:134) で
com.genious.Employee.main(Employee.java:54)で//
public class Employee implements Comparator
{
String firstname;
String lastname;
int mobileno;
public Employee(String firstname,String lastname,int mobileno)
{
this.firstname=firstname;
this.lastname=lastname;
this.mobileno=mobileno;
}
@Override
public String toString() {
return firstname;
}
@Override
public int compare(Object o1, Object o2) {
Employee e2=(Employee)o1;
Employee e3=(Employee)o2;
int i=e2.firstname.compareTo(e3.firstname);
if(i!=0)
return i;
return i;
}
/**
* @param args
*/
public static void main(String[] args) {
ArrayList list=new ArrayList();
Employee e=new Employee("anand","pandey",93456666);
Employee e1=new Employee("sheel","nidhi",678956344);
Employee e5=new Employee("shumit", "Kumar", 97390267);
Employee e6=new Employee("Kamal", "Kumar", 97390267);
list.add(e);
list.add(e1);
list.add(e5);
list.add(e6);
System.out.println(list);
Collections.sort(list);
System.out.println(list);
}
}