-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain3.java
More file actions
73 lines (55 loc) · 1.82 KB
/
Copy pathMain3.java
File metadata and controls
73 lines (55 loc) · 1.82 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
70
71
72
73
import java.util.*;
class Student {
private int id;
private String firstName;
private double cgpa;
public Student(int id, String firstName, double cgpa) {
this.id = id;
this.firstName = firstName;
this.cgpa = cgpa;
}
public int getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public double getCgpa() {
return cgpa;
}
}
class StudentComparator implements Comparator<Student> {
@Override
public int compare(Student s1, Student s2) {
if (s1.getCgpa() != s2.getCgpa()) {
// Sort by cgpa in descending order
return Double.compare(s2.getCgpa(), s1.getCgpa());
} else if (!s1.getFirstName().equals(s2.getFirstName())) {
// If cgpa is equal, sort by firstName in alphabetical order
return s1.getFirstName().compareTo(s2.getFirstName());
} else {
// If cgpa and firstName are equal, sort by id in ascending order
return Integer.compare(s1.getId(), s2.getId());
}
}
}
public class Main3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Student> students = new ArrayList<>();
int n = scanner.nextInt();
scanner.nextLine();
for (int i = 0; i < n; i++) {
int id = scanner.nextInt();
String firstName = scanner.next();
double cgpa = scanner.nextDouble();
students.add(new Student(id, firstName, cgpa));
}
// Sort students
Collections.sort(students, new StudentComparator());
for (Student student : students) {
System.out.println(student.getFirstName());
}
scanner.close();
}
}