Commit 55041b0
committed
feat(ComparatorDemo): add custom comparator sorting students by GPA desc then Name asc
What
- Added Student class:
- Fields: name (String), gpa (double).
- Constructor, getters for name and gpa.
- Added ComparatorDemo class:
- Creates list of Student objects: Alice (3.5), Bob (3.1), Charlie (3.9), Akshit (3.4), Smith (3.4).
- Demonstrates sorting with a custom Comparator via lambda:
- Primary: GPA descending.
- Secondary: Name ascending (alphabetical).
- Prints sorted students to stdout.
Why
- Shows how to implement flexible, context-specific ordering without modifying Student class.
- Highlights Comparator as external strategy for sorting compared to Comparable (natural ordering).
- Demonstrates dictionary-order comparison of Strings with compareTo().
How
- Used Students.sort((o1, o2) -> {...}) lambda comparator.
- GPA comparison: o2.getGpa() - o1.getGpa() for descending order.
- If GPAs equal: fallback to o1.getName().compareTo(o2.getName()) for ascending lex order.
- Iterated and printed each student’s name and GPA after sorting.
Logic
- Inputs: list of 5 students.
- Outputs:
Sorted order:
1. Charlie (3.9)
2. Alice (3.5)
3. Akshit (3.4)
4. Smith (3.4)
5. Bob (3.1)
- Flow:
1. Sort list using custom comparator.
2. Comparator returns +1 / -1 depending on GPA difference.
3. On equal GPAs, compareTo() ensures alphabetical order.
4. Print sorted list.
- Edge cases:
- Floating point subtraction can be imprecise; safer to use Double.compare().
- Equal GPA and equal name → comparator returns 0; considered equal by sorting.
- Complexity / performance: O(n log n).
- Concurrency / thread-safety: Not thread-safe; demo is local and safe.
- Error handling: Not applicable.
Real-life applications
- Ranking students by GPA while ensuring stable alphabetical order for ties.
- Useful in education systems, leaderboards, reporting tools.
- Comparator lambdas allow context-specific sorting without modifying entity class.
Notes
- Using Double.compare(o2.getGpa(), o1.getGpa()) is clearer and avoids floating-point pitfalls.
- Comparator.comparingDouble(Student::getGpa).reversed().thenComparing(Student::getName) offers a cleaner functional alternative.
- Current implementation demonstrates verbose lambda logic for educational clarity.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 447d160 commit 55041b0
File tree
1 file changed
+21
-27
lines changed- Section 25 Collections Frameworks/List Interface/ArrayList/Comparator/src
1 file changed
+21
-27
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | 1 | | |
4 | 2 | | |
5 | 3 | | |
6 | | - | |
| 4 | + | |
7 | 5 | | |
8 | 6 | | |
9 | 7 | | |
10 | 8 | | |
11 | | - | |
| 9 | + | |
12 | 10 | | |
13 | 11 | | |
14 | 12 | | |
| |||
39 | 37 | | |
40 | 38 | | |
41 | 39 | | |
42 | | - | |
43 | | - | |
| 40 | + | |
44 | 41 | | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
63 | 58 | | |
64 | 59 | | |
65 | | - | |
66 | | - | |
| 60 | + | |
67 | 61 | | |
68 | 62 | | |
69 | 63 | | |
70 | | - | |
| 64 | + | |
0 commit comments