Skip to content

Commit 55041b0

Browse files
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

1 file changed

+21
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
package ListDemo.ArrayListLinkedListStack;
2-
31
import java.util.ArrayList;
42
import java.util.List;
53

6-
class Student{
4+
class Student {
75
private String name;
86

97
private double gpa;
108

11-
public Student(String name, double gpa){
9+
public Student(String name, double gpa) {
1210
this.name = name;
1311
this.gpa = gpa;
1412
}
@@ -39,32 +37,28 @@ public static void main(String[] args) {
3937
// This comparator sorts the students primarily by descending GPA.
4038
// If two students have the same GPA, it sorts them alphabetically by name.
4139

42-
Students.sort((o1,o2)->
43-
{
40+
Students.sort((o1,o2)-> {
4441
// Calculate the difference in GPA (note: o2 - o1 gives descending order).
45-
if(o2.getGpa() - o1.getGpa() > 0)
46-
{
47-
// If o2 has a higher GPA, it should come before o1.
48-
return 1;
49-
}
50-
else if (o2.getGpa() - o1.getGpa() < 0)
51-
{
52-
// If o1 has a higher GPA, it should come before o2.
53-
return -1;
54-
}
55-
else
56-
{
57-
// If GPAs are equal, sort by name in ascending (alphabetical) order.
58-
return o1.getName().compareTo(o2.getName());
59-
//Lexicographically compare kar raha hai...words ko dictionary order (alphabetical order)
60-
//mein compare kiya ja raha hai. Java mein, jab hum String ke compareTo() method ko call karte hain,
61-
//toh woh dono strings ke characters ko ek-ek karke compare karta hai
62-
}
42+
if(o2.getGpa() - o1.getGpa() > 0) {
43+
// If o2 has a higher GPA, it should come before o1.
44+
return 1;
45+
}
46+
47+
else if (o2.getGpa() - o1.getGpa() < 0) {
48+
// If o1 has a higher GPA, it should come before o2.
49+
return -1;
50+
}
51+
else {
52+
// If GPAs are equal, sort by name in ascending (alphabetical) order.
53+
return o1.getName().compareTo(o2.getName());
54+
//Lexicographically compare kar raha hai...words ko dictionary order (alphabetical order)
55+
//mein compare kiya ja raha hai. Java mein, jab hum String ke compareTo() method ko call karte hain,
56+
//toh woh dono strings ke characters ko ek-ek karke compare karta hai
57+
}
6358
});
6459

65-
for(Student s : Students)
66-
{
60+
for(Student s : Students) {
6761
System.out.println(s.getName()+": "+s.getGpa());
6862
}
6963
}
70-
}
64+
}

0 commit comments

Comments
 (0)