按工资排序:比较器显示没有错误,但不起作用

Sorting by Salary: Comparator shows no error, but doesn't work

请帮助我理解我所缺少的。 我需要按薪水对我的员工进行排序。尝试通过 Comparator 来完成。没有显示错误,但对输出没有影响。

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));

查看我测试过的代码:Online Salary Comparator

您的代码看起来不错。
我试过你的代码,它工作正常。
排序后需要打印您的详细信息。
我试过如下

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);
        }
    }
}

输出

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