Java 按薪资排序:比较器没有显示错误,但不起作用
请帮我理解,我错过了什么。 我需要按工资对我的员工进行分类。试图通过比较器来实现。显示无错误,但对输出没有影响Java 按薪资排序:比较器没有显示错误,但不起作用,java,comparator,Java,Comparator,请帮我理解,我错过了什么。 我需要按工资对我的员工进行分类。试图通过比较器来实现。显示无错误,但对输出没有影响 public class FirstActivity { public static void main(String[] args) { //collection of employee Employee e1 = new Employee(1, "TestLead", "Microsoft", 4000.00, "
public class FirstActivity {
public static void main(String[] args) {
//collection of employee
Employee e1 = new Employee(1, "TestLead", "Microsoft", 4000.00, "Peter", "Doe", 35);
Employee e2 = new Employee(2, "ManualTester", "Accenture", 1800.00, "Carl", "Fox", 56);
Employee e3 = new Employee(3, "Developer", "IBM", 2100.00, "Michele", "Gilmore", 34);
Employee e4 = new Employee(4, "Manager", "IBM", 2500.00, "Alfred", "Pink", 23);
Employee e5 = new Employee(5, "AutomationTester", "Accenture", 2600.00, "Sam", "Bow", 40);
// Initialise ArrayList
List<Employee> employeeList = new ArrayList<Employee>();
// add values
employeeList.add(e1);
employeeList.add(e2);
employeeList.add(e3);
employeeList.add(e4);
employeeList.add(e5);
// print values
System.out.println("Workers salaries: ");
Iterator<Employee> itr = employeeList.iterator();
//traverse elements of ArrayList object
while(itr.hasNext())
{
Employee st=(Employee)itr.next();
System.out.println("Employees " + st.firstName + " salary is "+st.salary);
}
// trying to SORT by salary through this line
employeeList.sort(Comparator.comparing(Employee::getSalary));
}
}
你的代码正在运行。您所做的不正确的事情是您正在打印列表,或者您需要在排序和比较器使用后再次打印列表
// trying to SORT by salary through this line
employeeList.sort(Comparator.comparing(Employee::getSalary));
employeeList.forEach(employee -> System.out.println("Employees " + employee.firstName + " salary is "+employee.salary));
查看我测试过的代码:您的代码看起来不错。
我已经试过你的代码,它工作得很好。
排序后,您需要打印详细信息。
我试过下面的方法
public class FirstActivity {
public static void main(String[] args) {
// collection of employee
Employee e1 = new Employee(1, "TestLead", "Microsoft", 4000.00, "Peter", "Doe", 35);
Employee e2 = new Employee(2, "ManualTester", "Accenture", 1800.00, "Carl", "Fox", 56);
Employee e3 = new Employee(3, "Developer", "IBM", 2100.00, "Michele", "Gilmore", 34);
Employee e4 = new Employee(4, "Manager", "IBM", 2500.00, "Alfred", "Pink", 23);
Employee e5 = new Employee(5, "AutomationTester", "Accenture", 2600.00, "Sam", "Bow", 40);
// Initialise ArrayList
List<Employee> employeeList = new ArrayList<>();
// add values
employeeList.add(e1);
employeeList.add(e2);
employeeList.add(e3);
employeeList.add(e4);
employeeList.add(e5);
// print values
System.out.println("Workers salaries before sorting: ");
Iterator<Employee> itr = employeeList.iterator();
// traverse elements of ArrayList object
while (itr.hasNext()) {
Employee st = (Employee) itr.next();
System.out.println("Employees " + st.firstName + " salary is "+st.salary);
}
// trying to SORT by salary through this line
employeeList.sort(Comparator.comparing(Employee::getSalary));
System.out.println("\nWorkers salaries after sorting: ");
Iterator<Employee> itr1 = employeeList.iterator();
// traverse elements of ArrayList object
while (itr1.hasNext()) {
Employee st = (Employee) itr1.next();
System.out.println("Employees " + st.firstName + " salary is "+st.salary);
}
}
}
这与javascript中的排序不同,排序列表是从调用中返回的。对不起,您所说的调用是什么意思?不幸的是,我的排序输出没有变量。有什么建议吗?谢谢。方法“sort”返回一个列表,所以如果你想要排序的列表,你必须将返回的值赋给一个变量,可能是同一个实例。您想在哪里打印已排序的列表?上面的代码以对sort的调用返回nothing结束,该调用上面的任何输出都不可能受到影响。在sort再次打印列表后,您将看到更改!对输出没有影响为什么输出后执行的排序会影响输出?非常感谢您的帮助!非常感谢你的建议!我很感激!这是我的荣幸。
Workers salaries before sorting:
Employees TestLead salary is 4000.0
Employees ManualTester salary is 1800.0
Employees Developer salary is 2100.0
Employees Manager salary is 2500.0
Employees AutomationTester salary is 2600.0
Workers salaries after sorting:
Employees ManualTester salary is 1800.0
Employees Developer salary is 2100.0
Employees Manager salary is 2500.0
Employees AutomationTester salary is 2600.0
Employees TestLead salary is 4000.0