-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComparatorPerson.java
More file actions
43 lines (38 loc) · 1.55 KB
/
ComparatorPerson.java
File metadata and controls
43 lines (38 loc) · 1.55 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
/**
* The ComparatorPerson class represents a person with height and weight attributes.
* It provides a constructor to initialize these attributes.
*
* In the main method, an array of ComparatorPerson objects is created and sorted
* using a custom Comparator. The sorting is performed first by height, and then by
* weight if the heights are equal.
*
* After sorting, the list of people is printed in order of increasing height and weight.
*/
public class ComparatorPerson {
int height;
int weight;
public ComparatorPerson(int height, int weight) {
this.height = height;
this.weight = weight;
}
public static void main(String args[]) {
ComparatorPerson p1 = new ComparatorPerson(170, 70);
ComparatorPerson p2 = new ComparatorPerson(180, 80);
ComparatorPerson p3 = new ComparatorPerson(170, 75);
ComparatorPerson[] people = { p1, p2, p3 };
java.util.Arrays.sort(people, new java.util.Comparator<ComparatorPerson>() {
@Override
public int compare(ComparatorPerson a, ComparatorPerson b) {
int heightComparison = Integer.compare(a.height, b.height);
if (heightComparison != 0) {
return heightComparison;
}
return Integer.compare(a.weight, b.weight);
}
});
System.out.println("Sorted people by height and weight:");
for (ComparatorPerson p : people) {
System.out.println("Height: " + p.height + ", Weight: " + p.weight);
}
}
}