Skip to content

Commit 62c3e10

Browse files
committed
feat(collections): demonstrate sorting custom objects with lambda-based Comparator
What - Added `Main.java` in `Package3` to illustrate sorting of custom objects (`Student`) using `Collections.sort` with a comparator. - Defined `Student` as a static inner class with fields: - `id` (Integer) - `name` (String) - Created a list of students and applied a **custom comparator** to sort them in descending order of `id`. - Overrode `toString()` in `Student` for readable output (e.g., `33: Donald`). Why - By default, Java’s `Collections.sort()` relies on natural ordering (like integers, strings). - For custom objects, we must explicitly define sorting rules via: - Implementing `Comparable<T>` (natural order), or - Providing a `Comparator<T>` (custom order). - This example shows how to sort domain-specific objects (like students) with a comparator inline via lambda. Logic 1. Constructed 3 students with different IDs and names. 2. Added them to a `List<Student>`. 3. Used `Collections.sort(li, (a, b) -> b.id - a.id)`: - Comparator subtracts `id` values → sorts descending by ID. - Demonstrates custom ordering beyond natural sorting. 4. Printed the sorted list, relying on overridden `toString()` for formatted display. Key Takeaways ✔ Custom objects don’t have inherent ordering — you must define it. ✔ Comparator lambdas provide concise, inline sorting logic. ✔ `(a, b) -> b.id - a.id` → descending sort by ID. ✔ For ascending order: `(a, b) -> a.id - b.id`. ✔ `toString()` improves debugging/logging by showing meaningful output. ✔ Collections framework integrates seamlessly with lambdas introduced in Java 8. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent dd5ef5e commit 62c3e10

File tree

1 file changed

+39
-0
lines changed
  • Java 8 Crash Course/Lambda Expression/Comparator Using Lambda Expression/src/Package3

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package Package3;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
public class Main {
8+
public static void main(String[] args) {
9+
Student s1 = new Student(2, "Jackson");
10+
Student s2 = new Student(3, "Stark");
11+
Student s3 = new Student(33, "Donald");
12+
13+
List<Student> li = new ArrayList<Student>();
14+
li.add(s1);
15+
li.add(s2);
16+
li.add(s3);
17+
18+
// custom objects ko sort kar rahe hai haam.
19+
// We need to create our own Comparator.
20+
// We need to define how to sort custom objects or data it is not Integers that sort Natural order.
21+
Collections.sort(li, (a,b) -> b.id - a.id); // .id is custom for Objects.
22+
System.out.println(li);
23+
}
24+
25+
static class Student {
26+
public Integer id;
27+
28+
public String name;
29+
30+
public Student(Integer id, String name) {
31+
this.id = id;
32+
this.name = name;
33+
}
34+
35+
public String toString(){
36+
return this.id + ": "+ this.name;
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)