gpt4 book ai didi

java - 按照与另一个数组 java 相同的顺序对列表对象进行排序

转载 作者:行者123 更新时间:2023-12-02 08:41:46 26 4
gpt4 key购买 nike

您好,我想订购部门对象的员工列表,但我在数组中存储了顺序,这里是对象和示例

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

非常感谢您能帮助我

最佳答案

您可以按如下方式进行:

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]]

关于java - 按照与另一个数组 java 相同的顺序对列表对象进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61347728/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com