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.*;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--;}Collections.sort(studentList,Comparator.comparing(Student::getCgpa).reversed().thenComparing(Student::getFname).thenComparing(Student::getId));for(Studentst:studentList){System.out.println(st.getFname());}}}
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:
" :: " stands for Method Reference .It is most commonly used for instance methods of a class.As you might have observed ,unless you have something like this:
d -> d.getCgpa()>10 in filter operations ,in all other(most commonly) operations viz sorted,map etc. you can use :: like:
.map(Dish::getCalories) .
A wonderful example would be for forEach : .forEach(System.out::println) !!
Comparator.comparing creates a Comparator, which can be used to sort a collection or stream. If you don't provide a Comparator to the sorting method, it will rely on the natural ordering of the elements of the collection instead. A natural ordering is given only when the element class does implement the Comparable interface and in this case the ordering is defined by overriding the compareTo method.
In java number are, by default, sorted ascending order ( from smal to big).
when whe say reverse, we want the sorting to be descending (from big to smal)
It sorts in decreasing order the output we get from Collections.sort(studentList, Comparator.comparing(Student :: getCgpa)
.As you might see ' comparing ' does bydefault increasing order sorting
Nice - but how would you write unit tests that exercise all the combinations of the sorting? Making the Student class Comparable, or at least having a separate Comparator, would help greatly with testing.
Java Sort
You are viewing a single comment's thread. Return to all comments →
Using Java 8
java8 rules
here is Java Sort problem solution https://programs.programmingoneonone.com/2021/02/hackerrank-java-sort-solution.html
Here are my few different solutions in Java. 100% all test cases will pass
I hope you will find my answer useful.
Hackerrank Java Sort Solution
updated solution is here
https://www.thecscience.com/2021/05/HackerRank-java-sort-problem-solution.html
+1 for using Java 8 features.
I think it's better to use the primitive specializations of
Comparator.comparing()
for primitive keys, that iscomparingDouble()
for the CGPA andcomparingInt()
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:
Here is my solution:
what does that :: stands for in Student :: getCgpa
The double colon syntax is new to java 8 and lambdas. It points to functions and is equivalent to the alternative lambda -> expression
" :: " stands for Method Reference .It is most commonly used for instance methods of a class.As you might have observed ,unless you have something like this: d -> d.getCgpa()>10 in filter operations ,in all other(most commonly) operations viz sorted,map etc. you can use :: like:
.map(Dish::getCalories) . A wonderful example would be for forEach : .forEach(System.out::println) !!
Comparator.comparing basically takes place of overriding the compareTo method, correct?
Kind of, yes. To be more precise:
Comparator.comparing creates a Comparator, which can be used to sort a collection or stream. If you don't provide a Comparator to the sorting method, it will rely on the natural ordering of the elements of the collection instead. A natural ordering is given only when the element class does implement the Comparable interface and in this case the ordering is defined by overriding the compareTo method.
So, yes it does - basically.
Hi, Comparator.comparing(Student :: getCgpa).reversed().
Why are you using reversed() for only Cgpa comparison?
In java number are, by default, sorted ascending order ( from smal to big). when whe say
reverse
, we want the sorting to be descending (from big to smal)How to do the same thing in Java 7? It is showing some erros in java 7
It sorts in decreasing order the output we get from Collections.sort(studentList, Comparator.comparing(Student :: getCgpa) .As you might see ' comparing ' does bydefault increasing order sorting
May as well go all the way and use a stream
why you have used testCases--;
Issue solved..really silly from my end!!!
Amazing
I implemented in a basic way.
int testCases = Integer.parseInt(in.nextLine());
Can you explain this solution Collection.sort() line
instead of while loop if i try for loop i got wrong answer. can anyone help me to write for loop for program.
Nice - but how would you write unit tests that exercise all the combinations of the sorting? Making the Student class Comparable, or at least having a separate Comparator, would help greatly with testing.
This solution shows wrong answer.
It makes compilation error as I tried. It seems that this code has no syntax error, maybe hackerrank system has a problem.