Instagram
youtube
Facebook
Twitter

Java Sort program using Java HackerRank solutions

Problem

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

Solution

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

class StudentComparator implements Comparator<Student> {
    @Override
    public int compare(Student s1, Student s2) {
        if (s2.getCgpa() != s1.getCgpa()) {
            return Double.compare(s2.getCgpa(), s1.getCgpa());
        } else if (!s1.getFname().equals(s2.getFname())) {
            return s1.getFname().compareTo(s2.getFname());
        } else {
            return Integer.compare(s1.getId(), s2.getId());
        }
    }
}

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);
			studentList.add(st);
			
			testCases--;
		}
        
        Collections.sort(studentList, new StudentComparator());

        
      	for(Student st: studentList){
			System.out.println(st.getFname());
		}
	}
}

Steps involved in this solution:

1. The code starts with the definition of a Student class that represents a student's information. This class has private fields for student ID, first name, and CGPA, along with a constructor and getter methods.

2. Next, a StudentComparator class is defined, implementing the Comparator interface for custom sorting. The compare method is overridden to define the sorting criteria. It first sorts students by CGPA in descending order using Double.criteria. If CGPA values are equal, it sorts by first name in alphabetical order using String.compareTo. If CGPA and first names are the same, it sorts by student ID using Integer.compare.

3. The Solution class is defined, which contains the main method, serving as the entry point of the program.

4. Inside the main method, a Scanner object (in) is created to read input from the console. An integer variable testcases is used to store the number of student records.

5. A list called studentList is initialized using the ArrayList class. This list will store student records.

6. A while loop is used to read student records based on the value of testcases. Inside the loop, the code reads the student ID, first name, and CGPA from the input. A new Student object is created with these values, and it is added to the studentList.

7. After reading all student records, the studentList is sorted using the Collections.sort method. The StudentComparator is used as the custom comparator to sort the list based on the defined criteria.

8. Another for-each loop is used to iterate through the sorted studentList. Inside the loop, the first name of each student is printed using st.getFname().

9. The Scanner is closed to release resources. The program execution ends.