import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Sorter1 {
public static void main(String[] args) {
List students = new ArrayList<>();
students.add(new Student("A", 10));
students.add(new Student("B", 12));
students.add(new Student("C", 8));
students.add(new Student("D", 9));
students.add(new Student("E", 11));
students.add(new Student("F", 14));
System.out.println("=== Before Sort ============");
students.forEach(i -> System.out.println(i.toString()));
List list1 = students.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());
// OR
List list1 = students.stream().sorted((a,b) -> a.getAge().compareTo(b.getAge())).collect(Collectors.toList());
System.out.println("\n=== After Sort ============");
list1.forEach(i -> System.out.println(i.toString()));
}
}
Leave a Reply