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.
//Complete the code//implemented the solution using Comparator classGPAComparatorimplementsComparator<Student>{publicintcompare(Students1,Students2){intcompareCGPA=Double.compare(s2.getCgpa(),s1.getCgpa());if(compareCGPA!=0){returncompareCGPA;}else{returns1.getFname().compareTo(s2.getFname());}}}publicclassSolution{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--;}//this line of solution is required to compare studentListCollections.sort(studentList,newGPAComparator());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 →