基本上,我有一个名为“Employees”的容器类,其中包含一个 ArrayList。此 ArrayList 包含“Employee”对象,这些对象又包含“EmployeeData”对象,后者又包含 String 对象,例如“first”或“last”(它们是员工姓名)。
这是 ArrayList 结构的示意图:
ArrayList[Employee] emps ==> 1:Many ==> Employee emp
Employee emp ==> 1:1 ==> EmployeeData data
EmployeeData data ==> 1:2 ==> String last // A string that contains employee's last name.
我到底如何对 ArrayList 执行快速排序,以便其中的“Employee”对象根据字符串对象“last”按字母顺序排列?好像有点复杂!
这是我的课程的基本设计:
class Employees{
//data:
private ArrayList<Employee> emps = new ArrayList<Employee>();
//Some constructors go here
//Methods to add, remove, toString, etc, go here
public /*output a sorted ArrayList?*/ sort(){
// Some kind of "quicksort" in here to modify or create a new ArrayList sorted by employee's las name...
}
}
class Employee{
//data:
EmployeeData data;
// Some methods to construct and modify EmployeeData data.
}
class EmployeeData{
//data:
String first, last; // I wish to sort with "last". How do you do it?
double payrate, hours;
//...methods...
}
如您所见,这些是类。我不知道如何在“Employees”类中实现“sort”,以便通过“EmployeeData”类的“last”变量对 ArrayList 进行排序。