Здравствуйте, я хочу заказать список сотрудников для объектов отдела, но у меня есть порядок в массиве здесь объектов и пример
public class Employee {
private String name;
private int age;
private double salary;
private Department department;
public Employee(String name, int age, double salary, Department department) {
...
}
// standard getters, setters and toString
}
public class Department {
private Integer id;
private String name;
public Department(Integer id, String name) {
...
}
// standard getters, setters and toString
}
Department[] departments = new Department[] {
new Department(1, "Computing" ), new Department(2, "Human Resources"),
new Department(3, "administration"), new Department(4, "operations"),
new Department(5, "marketing"), new Department(6, "communication")
};
Employee[] employees = new Employee[] {
new Employee("John", 23, 5000, departments[5]), new Employee("Steve", 26, 6000, departments[3]),
new Employee("Frank", 33, 7000,departments[4]), new Employee("Earl", 43, 10000, departments[2]),
new Employee("Jessica", 23, 4000, departments[1]), new Employee("Pearl", 33, 6000, departments[0])};
String[] arrOrderDepartment = new String[]{"marketing", "Computing", "administration", "Human Resources", "communication", "operations"};
нижняя строка сотрудники, заказанные по приказу организации отдела
employeesSortedByDepartment = [Employee{"Frank", 33, 7000, Department{id=5, name='marketing'}},Employee{ "Jessica", 23, 4000, Department{id=6, name='communication'} },Employee{"Steve", 26, 6000, Department{id=3, name='administration'} },Employee{"Earl", 43, 10000, Department{id=2, name='Human Resources'}},Employee{ "Pearl", 33, 6000, =Department{id=6, name='communication'} },Employee{ "John", 23, 5000, Department{id=4, name='operations'} }];
Я использовал что-то во время работы, но это не дает мне ожидаемый результат
Collections.sort(department, new Comparator<String>(){
public int compare(String left, String right) {
return arrOrderDepartment[stringList.indexOf(left)] - arrOrder[stringList.indexOf(right)];
}
});
Я использую java 6
большое спасибо за то, что вы можете мне помочь