Sobes.tech
Junior — Senior

Sorting the list of employees by increasing salary and displaying in a readable format

livecode

Task condition

In the project, a class Employee is defined, representing employee data. In the main method, a collection of objects of this class is created, filled with several records, and printed to the console. It is necessary to correct the output and also sort all employees by increasing their salary. How to implement the required?

class MyCode {
    public static void main(String[] args) {
        System.out.println("Hello Java");
        
        List<Employee> staff = new ArrayList<>();
        staff.add(new Employee("Ivan", 25, 500000));
        staff.add(new Employee("Kirill", 30, 600000));
        staff.add(new Employee("Stepan", 20, 750000));
        staff.add(new Employee("Maxim", 21, 250000));
        staff.add(new Employee("Leonid", 35, 1500000)); // corrected invalid character
        
        // sort by salary in ascending order
        staff.sort(Comparator.comparingInt(Employee::getSalary));
        
        for (Employee emp : staff) {
            System.out.println(emp.toString());
        }
    }
}