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.*;classStudentimplementsComparable<Student>{privateintid;privateStringfname;privatedoublecgpa;publicStudent(intid,Stringfname,doublecgpa){super();this.id=id;this.fname=fname;this.cgpa=cgpa;}publicintgetId(){returnid;}publicStringgetFname(){returnfname;}publicdoublegetCgpa(){returncgpa;}@OverridepublicintcompareTo(StudentanotherStudent){if(Double.compare(anotherStudent.getCgpa(),this.getCgpa())!=0){returnDouble.compare(anotherStudent.getCgpa(),this.getCgpa());}elseif(!this.getFname().equals(anotherStudent.getFname())){returnthis.getFname().compareTo(anotherStudent.getFname());}else{returnthis.getId()-anotherStudent.getId();}}}//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--;}in.close();Collections.sort(studentList);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 →