java list sort comparator date descending lambda

Solutions on MaxInterview for java list sort comparator date descending lambda by the best coders in the world

showing results for - "java list sort comparator date descending lambda"
Charlotte
02 May 2016
1myActionsDashboardDtoList.sort (Collections.reverseOrder(Comparator.comparing (MyActionsDashboardDto::getDateIn)));
Marta
16 Mar 2017
1Comparator sortingByName =
2(Student s1, Student s2)->s1.getName().compareTo(s2.getName());
Margaux
20 Aug 2019
1import java.util.ArrayList;
2import java.util.List;
3class Student {
4   String name; 
5   int age; 
6   int id; 
7   public String getName() {
8      return name; 
9   } 
10   public int getAge() { 
11      return age; 
12   } 
13   public int getId() { 
14      return id; 
15   } 
16   Student(String n, int a, int i){ 
17      name = n; 
18      age = a; 
19      id = i; 
20   } 
21   @Override public String toString() {     
22      return ("Student[ "+"Name:"+this.getName()+             
23              " Age: "+ this.getAge() +                     
24              " Id: "+ this.getId()+"]"); 
25   }
26}
27public class Example {
28   public static void main(String[] args) {
29      List<Student> studentlist = new ArrayList<Student>();
30      studentlist.add(new Student("Jon", 22, 1001)); 
31      studentlist.add(new Student("Steve", 19, 1003)); 
32      studentlist.add(new Student("Kevin", 23, 1005)); 
33      studentlist.add(new Student("Ron", 20, 1010)); 
34      studentlist.add(new Student("Lucy", 18, 1111));
35      System.out.println("Before Sorting the student data:"); 
36 
37      //java 8 forEach for printing the list 
38      studentlist.forEach((s)->System.out.println(s));
39
40      System.out.println("After Sorting the student data by Age:"); 
41
42      //Lambda expression for sorting by age 
43      studentlist.sort((Student s1, Student s2)->s1.getAge()-s2.getAge()); 
44
45      //java 8 forEach for printing the list
46      studentlist.forEach((s)->System.out.println(s));         
47
48      System.out.println("After Sorting the student data by Name:"); 
49      //Lambda expression for sorting the list by student name       
50      studentlist.sort((Student s1, Student s2)->s1.getName().compareTo(s2.getName())); 
51      studentlist.forEach((s)->System.out.println(s));        
52      System.out.println("After Sorting the student data by Id:");        
53      //Lambda expression for sorting the list by student id 
54      studentlist.sort((Student s1, Student s2)->s1.getId()-s2.getId()); 
55      studentlist.forEach((s)->System.out.println(s)); 
56   }
57}
similar questions