forked from VarshaDas/Java-Code-Snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComparatorDemo.java
More file actions
99 lines (55 loc) · 2.91 KB
/
ComparatorDemo.java
File metadata and controls
99 lines (55 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main.java;
import org.codewithease.javatopics.commons.Employee;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ComparatorDemo {
public static void main(String[] args) {
/*
Sort List of Objects
Custom Sorting Criteria
Multiple Sorting Criteria
Case-Insensitive Sorting
Reverse Order Sorting
*/
//1. Sort the strings based on their length in ascending order
List<String> fruits = Arrays.asList("apple", "cherry", "banana", "pineapple", "kiwi", "elderberry");
// fruits.sort(Comparator.comparingInt(String::length));
Collections.sort(fruits, Comparator.comparingInt(String::length));
System.out.println("fruits "+fruits);
// 2.Sort the list of integers in descending order and print the result
List<Integer> nums = Arrays.asList(3,2,90,34,21,12);
nums.sort(Comparator.reverseOrder());
System.out.println("nums in reverse order : "+nums);
// 3.Sort the list of employees based on their age in ascending order and print the result
List<Employee> employees = Arrays.asList(new Employee(1, "Varsha", 28, 3000),
new Employee(2, "Harsha", 21, 4000),
new Employee(3, "Tony", 21, 1000),
new Employee(4, "Ramesh", 30, 5000));
employees.sort(Comparator.comparingInt(Employee::getAge));
System.out.println("employees based on age "+employees);
// 4.Sort the list of employees based on their age in ascending order. If the ages are the same, compare by salary. Print the result.
employees.sort(Comparator.comparingInt(Employee::getAge).thenComparing(Employee::getSalary));
System.out.println(
"employees with multiple sort criteria "+employees
);
// 5.Sort the list of strings based on the index of the first occurrence of "e" in each string and print the result
fruits.sort(Comparator.comparingInt(e -> e.indexOf("e")));
System.out.println("fruits with index position sorting :: "+ fruits);
//6. Sort a list of strings ignoring case sensitivity using a case-insensitive comparator.
List<String> fruitsMix = Arrays.asList("APPLE", "cherry", "baNaNa", "pineapple", "KiWI", "elderberry");
Comparator<String> caseInsensitiveComp = String.CASE_INSENSITIVE_ORDER;
fruitsMix.sort(caseInsensitiveComp);
System.out.println(fruitsMix);
//7. Sort a list of dates in ascending order using the comparing() method with a lambda expression.
List<LocalDate> dates = Arrays.asList(
LocalDate.of(2023, 5, 10),
LocalDate.of(2023, 3, 15),
LocalDate.of(2023, 7, 1)
);
dates.sort(Comparator.comparing(date -> date));
System.out.println(dates);
}
}