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:



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.

hackerrank java sort solution

Java Sort problem solution | Hackerrank

import java.util.*;

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

class StudentComparator implements Comparator<Student>{

	public int compare(Student s1, Student s2) {

		double cgpa1 = s1.getCgpa();
		double cgpa2 = s2.getCgpa();

		if(Math.abs(cgpa1 - cgpa2)<.00000001){
			int fnameCom = s1.getFname().compareTo(s2.getFname());
				return s1.getId()-s2.getId(); 
			return fnameCom;

		return (cgpa1<cgpa2)?1:(cgpa1>cgpa2?-1:0);


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

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

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

		Collections.sort(studentList, new StudentComparator());
		for(Student st: studentList){