We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importjava.util.*;classStudentComparatorimplementsComparator<Student>{@Overridepublicintcompare(StudentfirstStudent,StudentsecondStudent){if(Double.compare(secondStudent.getCgpa(),firstStudent.getCgpa())==0){returnfirstStudent.getFname().compareTo(secondStudent.getFname());}returnDouble.compare(secondStudent.getCgpa(),firstStudent.getCgpa());}}classStudent{privateintid;privateStringfname;privatedoublecgpa;publicStudent(intid,Stringfname,doublecgpa){super();this.id=id;this.fname=fname;this.cgpa=cgpa;}publicintgetId(){returnid;}publicStringgetFname(){returnfname;}publicdoublegetCgpa(){returncgpa;}}//Complete the codepublicclassSolution{publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);inttestCases=Integer.parseInt(in.nextLine());List<Student>studentList=newArrayList<Student>();while(testCases>0){intid=in.nextInt();Stringfname=in.next();doublecgpa=in.nextDouble();Studentst=newStudent(id,fname,cgpa);studentList.add(st);testCases--;}StudentComparatorcomp=newStudentComparator();Collections.sort(studentList,comp);for(Studentst:studentList){System.out.println(st.getFname());}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Sort
You are viewing a single comment's thread. Return to all comments →