Question: Sort by Grade Given a List of Student objects named students and a Comparator called gradeComparator , sort each student by their grade ( highest

Sort by Grade
Given a List ofStudentobjects namedstudentsand a Comparator calledgradeComparator, sort each student by their grade (highest grades first).
Note:
Use thesort()method to compare the students' grades.
In the parameters of thesort()method, compare students using thegradeComparator
import java.io.*;
import java.util.*;
public class Student {
private String name;
private int grade;
public Student(String name, int grade){
this.name = name;
this.grade = grade;
}
public String getName(){
return this.name;
}
public int getGrade(){
return this.grade;
}
public static void main(String[] args){
List students = new ArrayList<>();
students.add(new Student("Alice",85));
students.add(new Student("Rob",92));
students.add(new Student("Charlie",78));
students.add(new Student("Janice",88));
students.add(new Student("Eva",95));
Comparator gradeComparator = Comparator.comparingInt(Student::getGrade);
/***** DO NOT CHANGE THE CODE ABOVE THIS LINE *****/
// WRITE YOUR CODE HERE
/***** DO NOT CHANGE THE CODE BELOW THIS LINE *****/
for (Student s : students){
System.out.println(s.getName()+"- Grade: "+ s.getGrade());
}
}
Expected STDOUT
Charlie-Grade:78
Alice-Grade:85
Janice-Grade:88
Rob-Grade:92
Eva-Grade:95

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!