class Employee implements Comparable{
private String name;
private String gender;
Employee(String name, String gender) {
this.name = name;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String toString() {
return this.getName() + " : " + this.getGender();
}
@Override
public int compareTo(Object o) {
int gendarComp=this.getGender().compareTo(((Employee)o).getGender());
return (gendarComp!=0?gendarComp:this.name.compareTo(((Employee)o).getName()));
}
}
class EmployeeExec {
public static void main(String[] args) {
List allEmpls = new ArrayList();
allEmpls.add(new Employee("vijay", "m"));
allEmpls.add(new Employee("balaji", "m"));
allEmpls.add(new Employee("shaifali", "f"));
allEmpls.add(new Employee("archana", "f"));
allEmpls.add(new Employee("alala", "m"));
allEmpls.add(new Employee("kiran", "f"));
sortEmployees(allEmpls);
}
public static void sortEmployees(List allEmpls) {
Collections.sort(allEmpls);
System.out.println(allEmpls);
}
}
Can any one Please help me in understanding how it is working? what is the funda to sort. I kept some sysout statements to see, but I dont really understand how it is getting called. I know its working with merge sort. but how? please help me.
-Balaji
No comments:
Post a Comment