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);
}
}
任何人可以幫我理解它是如何工作的嗎?什麼是基礎排序。我保留了一些sysout語句來看,但我真的不明白它是如何被調用的。我知道它使用合併排序。但是如何?請幫幫我。排序在多個領域 - 它的工作,但不能理解它是如何工作的。
-Balaji