Sort employee details using Stream


import java.util.ArrayList;
import java.util.List;


class Employee {
    private Integer id;
    private String name;

    public Employee(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    Employee() {

    }

}


public class EmployeeSort {

    public static void main(String[] args) {

        Employee e1 = new Employee(101, "abc");
        Employee e2 = new Employee(106, "def");
        Employee e3 = new Employee(104, "thi");
        Employee e4 = new Employee(102, "aac");
        List al = new ArrayList<>();
        al.add(e1);
        al.add(e4);
        al.add(e3);
        al.add(e2);
     
        al.stream().sorted((a, b) -> b.getName().compareTo(a.getName())).forEach(li -> {
            System.out.println(li.getName());
        });
       
        al.stream().sorted((a, b) -> b.getId().compareTo(a.getId())).forEach(li -> {
            System.out.println(li.getId());
        });
}
}

Comments