你好,我想订购部门对象的雇员列表,但在这里有对象和示例的数组中的订单
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
非常感谢您能为我提供的帮助
答案 0 :(得分:1)
您可以执行以下操作:
public class Main {
public static void main(String[] args) {
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" };
Employee[] employeesSortedByDepartment = new Employee[employees.length];
for (int i = 0; i < arrOrderDepartment.length; i++) {
employeesSortedByDepartment[i] = getEmployeeByDeptId(employees,
findDeptIdByDeptName(departments, arrOrderDepartment[i]));
}
for (Employee employee : employeesSortedByDepartment) {
System.out.println(employee);
}
}
static int findDeptIdByDeptName(Department[] departments, String departmentName) {
for (int i = 0; i < departments.length; i++) {
if (departments[i].getName().equalsIgnoreCase(departmentName)) {
return departments[i].getId();
}
}
return -1;
}
static Employee getEmployeeByDeptId(Employee[] employees, int id) {
for (Employee employee : employees) {
if (employee.getDepartment().getId() == id) {
return employee;
}
}
return null;
}
}
输出:
Employee [name=Frank, age=33, salary=7000.0, department=Department [id=5, name=marketing]]
Employee [name=Pearl, age=33, salary=6000.0, department=Department [id=1, name=Computing]]
Employee [name=Earl, age=43, salary=10000.0, department=Department [id=3, name=administration]]
Employee [name=Jessica, age=23, salary=4000.0, department=Department [id=2, name=Human Resources]]
Employee [name=John, age=23, salary=5000.0, department=Department [id=6, name=communication]]
Employee [name=Steve, age=26, salary=6000.0, department=Department [id=4, name=operations]]
答案 1 :(得分:0)
由于部门的排列已经在1-6的顺序中,因此您无需“排序”-您需要执行的是应用所需的顺序。
int[] arrOrderDepartment = {5, 1, 3, 2, 6, 4};
Department[] sortedDepartment = new Department[6];
for (int i = 0; i < 6; i++) {
sortedDepartment[i] = department[arrOrderDepartment[i] - 1]
}
现在sortedDepartment[0]
将指向部门5,sortedDepartment[1]
将指向部门1,依此类推。
答案 2 :(得分:0)
我认为您可以使用可比拟的Java 8(我相信)解决此问题。
这是此链接上的oracle文档。 https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html
此链接提供了您的问题的示例。 How to sort an array of objects in Java?
答案 3 :(得分:0)
感谢您的回答,昨天通过分析和测试,我得到的解决方案包含与答案Arvind Kumar Avinash类似的内容!给我了我的代码
public class Main {
public static void main(String[] args) {
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"),
new Department(7, "shopping"), new Department(6, "Inventory")
};
final 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]),
new Employee("Pearl", 33, 6000, departments[6]), new Employee("Pearl", 33, 6000, departments[7])
};
final String[] arrOrderDepartment = new String[]{"operations", "Computing", "administration", "Inventory", "Human Resources", "communication", "marketing"};
final List<Employee> employeeList = Arrays.asList(employees);
List<Employee> copyEmployeeList = Arrays.asList(employees);
Collections.sort(copyEmployeeList, new Comparator<Employee>(){
public int compare(Employee o1, Employee o2)
{
String a = getDepartmentOfList(o1.getDepartment().getName(), employeeList);
String b = getDepartmentOfList(o2.getDepartment().getName(), employeeList);
return indexOf(a, arrOrderDepartment) - indexOf(b, arrOrderDepartment);
}
});
System.out.println(copyEmployeeList);
}
private static String getDepartmentOfList(String name, List<Employee> employeeList) {
for (Employee employee: employeeList) {
if (name.equalsIgnoreCase(employee.getDepartment().getName())){
return employee.getDepartment().getName();
}
}
return null;
}
private static int indexOf( String c , String[] arr ) {
for( int i = 0 ; i < arr.length ; i++ ) {
if( arr[i].equalsIgnoreCase(c) ) {
return i;
}
}
return -1;
}
}
添加其他部门以查看代码的行为并首先添加它添加其他部门以查看代码的行为并首先添加代码