• + 0 comments

    +1 for using Java 8 features.

    I think it's better to use the primitive specializations of Comparator.comparing() for primitive keys, that is comparingDouble() for the CGPA and comparingInt() for the ID. This way we can avoid unneccessary boxing.

    My next thaught: "Consider implementing Comparable" (check Joshua Bloch's "Effective Java")?

    Finally, using Java 8 we could even read the input data and generate a sorted list in one go:

    class Student implements Comparable<Student> {
      // ...
      private static final Comparator<Student> comp =
        Comparator.comparingDouble(Student::getCgpa).reversed()
                  .thenComparing(Student::getFname)
                  .thenComparingInt(Student::getId);
      // ...
      @Override
      public int compareTo(final Student other) {
        return comp.compare(this, other);
      }
    }
    
    public class Solution {
      public static void main(final String[] args) {
        final Scanner in = new Scanner(System.in);
        final int numTestCases = in.nextInt();
        in.nextLine();
        
        final List<Student> students = Stream.generate(() ->
            new Student(in.nextInt(),
                        in.next(),
                        in.nextDouble())
          ).limit(numTestCases).sorted().collect(toList());
            
        for (Student student : students) {
          System.out.println(student.getFname());
        }
      }
    }