• + 0 comments
    //Complete the code
    //implemented the solution using Comparator 
    class GPAComparator implements Comparator<Student>{
        public int compare(Student s1, Student s2) {
            int compareCGPA = Double.compare(s2.getCgpa(), s1.getCgpa()); 
            if (compareCGPA != 0) {
                return compareCGPA;
            } else {
                return s1.getFname().compareTo(s2.getFname());
            }
        }
    }
    
    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--;
    		}
    				//this line of solution is required to compare studentList
            Collections.sort(studentList, new GPAComparator());
            
          	for(Student st: studentList){
    			System.out.println(st.getFname());
    		}
    	}