You've successfully subscribed to The Poor Coder | Hackerrank Solutions
Great! Next, complete checkout for full access to The Poor Coder | Hackerrank Solutions
Welcome back! You've successfully signed in.

## Hackerrank Java Sort Solution

Beeze Aal

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 , representing the total number of students. The next  lines contains a list of student information in the following structure:

ID Name CGPA


Constraints

The name contains only lowercase English letters. The  contains only integer numbers without leading zeros. The CGPA will contain, at most, 2 digits after the decimal point.

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
Samara
Samiha
Rumpa


### Solution in java8

Approach 1.

import java.io.*;
import java.util.*;

// practice with rainboy
public class JavaSort {
static class S implements Comparable<S> {
int id;
String name;
double gpa;
S(int id, String name, double gpa) {
this.id = id; this.name = name; this.gpa = gpa;
}
@Override public int compareTo(S s) {
if (gpa != s.gpa)
return gpa > s.gpa ? -1 : 1;
if (!name.equals(s.name))
return name.compareTo(s.name);
return id - s.id;
}
}
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter(System.out);
S[] ss = new S[n];
for (int i = 0; i < n; i++) {
int id = Integer.parseInt(st.nextToken());
String name = st.nextToken();
double gpa = Double.parseDouble(st.nextToken());
ss[i] = new S(id, name, gpa);
}
Arrays.sort(ss);
for (S s : ss)
pw.println(s.name);
pw.close();
}
}

Approach 2.

import java.util.*;

class Student{
private int id;
private String fname;
private double cgpa;
public Student(int id, String fname, double cgpa) {
super();
this.id = id;
this.fname = fname;
this.cgpa = cgpa;
}
public int getId() {
return id;
}
public String getFname() {
return fname;
}
public double getCgpa() {
return cgpa;
}
}

//Complete the code
public class Solution
{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int testCases = Integer.parseInt(in.nextLine());

List<Student> studentList = new ArrayList<Student>();
while(testCases>0){
int id = in.nextInt();
String fname = in.next();
double cgpa = in.nextDouble();

Student st = new Student(id, fname, cgpa);

testCases--;
}
Collections.sort(studentList,  Comparator.comparing(Student :: getCgpa).reversed().thenComparing(Student :: getFname).thenComparing(Student :: getId));
for(Student st: studentList){
System.out.println(st.getFname());
}
}
}


Approach 3.

import java.util.*;

class Student{
private int id;
private String fname;
private double cgpa;
public Student(int id, String fname, double cgpa) {
super();
this.id = id;
this.fname = fname;
this.cgpa = cgpa;
}
public int getId() {
return id;
}
public String getFname() {
return fname;
}
public double getCgpa() {
return cgpa;
}
}

//Complete the code
public class Solution
{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int testCases = Integer.parseInt(in.nextLine());

List<Student> studentList = new ArrayList<Student>();
while(testCases>0){
int id = in.nextInt();
String fname = in.next();
double cgpa = in.nextDouble();

Student st = new Student(id, fname, cgpa);