-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComparator2
More file actions
84 lines (73 loc) · 1.84 KB
/
Comparator2
File metadata and controls
84 lines (73 loc) · 1.84 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
74
75
76
77
78
79
80
81
82
83
84
/*
You are given a list of student information: ID, FirstName, and CGPA.
Your task is to rearrange them according to their CGPA in decreasing order.
If two student have the same CGPA, then arrange them according to their first name in alphabetical order.
If those two students also have the same first name, then order them according to their ID.
No two students have the same ID.
Input:
6
33 Rumpa 3.68
85 Ashis 3.85
56 Samiha 3.75
19 Samara 3.75
22 Fahim 3.76
20 Rumpa 3.68
Output:
Ashis 3.85 85
Fahim 3.76 22
Samara 3.75 19
Samiha 3.75 56
Rumpa 3.68 20
Rumpa 3.68 33
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
class Check implements Comparator<Student> {
@Override
public int compare(Student a, Student b) {
if (a.getGpa()==b.getGpa()) {
if (a.getName().equals(b.getName())) {
return a.getId() - b.getId();
}
else
return a.getName().compareTo(b.getName());
}
else
return b.getGpa() > a.getGpa() ? 1 : -1;
}
}
class Student {
private int id;
private String name;
private double gpa;
public Student(int id, String name, double gpa) {
this.id = id;
this.name = name;
this.gpa = gpa;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getGpa() {
return gpa;
}
}
public class Solution
{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Student> l = new ArrayList<>();
Check c = new Check();
for (int i=0; i<n; i++)
l.add(new Student(sc.nextInt(), sc.next(), sc.nextDouble()));
Collections.sort(l, c);
for(Student st : l)
System.out.printf("%-7s %.2f %d\n", st.getName(), st.getGpa(), st.getId());
}
}