Hello I have I want to order a list of employee for department objects but I have the order in an array here the objects and the example
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"};
the bottom line is the employees ordered by the order of the department arrangement
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'} }];
I have used something while working but it does not give me the expected result
Collections.sort(department, new Comparator<String>(){
public int compare(String left, String right) {
return arrOrderDepartment[stringList.indexOf(left)] - arrOrder[stringList.indexOf(right)];
}
});
i am using java 6
thank you very much in what you can help me