-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP54_SortCustomObjectsComparable.java
More file actions
69 lines (57 loc) · 2.34 KB
/
Copy pathP54_SortCustomObjectsComparable.java
File metadata and controls
69 lines (57 loc) · 2.34 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
package programs;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* ============================================================
* PROGRAM 54: Sort Custom Objects with Comparable and Comparator
* ============================================================
* Problem: WAP to sort a List of `Developer` objects:
* a) By ID (Natural Order using `Comparable<Developer>`)
* b) By Salary Descending (using `Comparator`)
* c) By Name Alphabetically (using Lambda Comparator)
* ============================================================
*/
class Developer implements Comparable<Developer> {
private int id;
private String name;
private double salary;
public Developer(int id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
public int getId() { return id; }
public String getName() { return name; }
public double getSalary() { return salary; }
@Override
public int compareTo(Developer o) {
return Integer.compare(this.id, o.id); // Ascending ID
}
@Override
public String toString() {
return String.format("Developer[ID=%3d, Name='%-15s', Salary=$%,9.2f]", id, name, salary);
}
}
public class P54_SortCustomObjectsComparable {
public static void main(String[] args) {
List<Developer> team = new ArrayList<>();
team.add(new Developer(105, "Yodha Raja", 125000.0));
team.add(new Developer(101, "Sarah Connor", 140000.0));
team.add(new Developer(103, "Alex Mercer", 98000.0));
team.add(new Developer(102, "David Kim", 115000.0));
// 1. Natural Sort (Comparable - By ID)
System.out.println("=== 1. SORTED BY ID (NATURAL ORDER) ===");
Collections.sort(team);
team.forEach(d -> System.out.println(" " + d));
// 2. Sort by Salary Descending (Comparator)
System.out.println("\n=== 2. SORTED BY SALARY DESCENDING ===");
team.sort((d1, d2) -> Double.compare(d2.getSalary(), d1.getSalary()));
team.forEach(d -> System.out.println(" " + d));
// 3. Sort by Name Alphabetically
System.out.println("\n=== 3. SORTED BY NAME (A-Z) ===");
team.sort(Comparator.comparing(Developer::getName));
team.forEach(d -> System.out.println(" " + d));
}
}