-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava_Comparator_HackerRank.java
More file actions
136 lines (94 loc) · 3.21 KB
/
Java_Comparator_HackerRank.java
File metadata and controls
136 lines (94 loc) · 3.21 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package technicals;
/*
* 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.
Hint: You can use comparators to sort a list of objects. See the oracle docs to learn about comparators.
Input Format
The first line of input contains an integer n , representing the total number of students. The next n lines contains a list of student information in the following structure:
ID Name CGPA
Output Format
After rearranging the students according to the above rules, print the first name of each student on a separate line.
Sample Input
5
33 Rumpa 3.68
85 Ashis 3.85
56 Samiha 3.75
19 Samara 3.75
22 Fahim 3.76
Sample Output
Ashis
Fahim
Samiha
Samara
Rumpa
* */
import java.util.*;
//class 1
class Student {
// Attributes of student
String Name;
int id;
double cgpa;
// Parameterized constructor
public Student( Integer id, String Name, Double cgpa)
{
// This keyword refers to current instance itself
this.Name = Name;
this.id = id;
this.cgpa=cgpa;
}
// Getter setter methods
public String getName() { return Name; }
public void setName(String Name) { this.Name = Name; }
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public Double getcgpa() {return cgpa;}
public void setcgpa(Double cgpa) {this.cgpa=cgpa;}
// Overriding toString() method
@Override
public String toString()
{
return Name ;
}
}
// Class 2
// Helper class implementing Comparator interface
class StudentSortingComparator implements Comparator<Student> {
// Method 1
// To compare customers
@Override
public int compare(Student std1,Student std2)
{
// Comparing customers
int NameCompare = std1.getName().compareTo(std2.getName());
int IdCompare = std1.getId().compareTo(std2.getId());
int CGPACompare = std1.getcgpa().compareTo(std2.getcgpa());
if(CGPACompare == 0 )
{
if(IdCompare ==0)
return NameCompare;
else
return IdCompare;
}
return CGPACompare;
}
}
public class HackerRank_Java_Sort {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
ArrayList<Student> list = new ArrayList<>();//Creating an empty ArrayList of Student type
int n = sc.nextInt();
//System.out.println("Enter the student details: \nIn the format of ID, Name, CGPA");
for(int i =1;i<=n;i++)
{
list.add(new Student(sc.nextInt(),sc.next(),sc.nextDouble()));
}
System.out.println("\nAfter Sorting in Descending Format:");
Collections.sort(list, new StudentSortingComparator());
Collections.reverse(list);
for(Student i :list)
{
System.out.println(i);
}
}
}