You are viewing a single comment's thread. Return to all comments →
import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Scanner;
public class Solution { public static void main(String[] args) {
List<Student> students = new ArrayList<>(); Scanner scanner = new Scanner(System.in); int customersTotalCount = scanner.nextInt(); for (int i = 0; i < customersTotalCount; i++) { int customerId = scanner.nextInt(); String custName = scanner.next(); double cgpa = scanner.nextDouble(); students.add(new Student(customerId, custName, cgpa)); } students.sort(new GPAComparator()); students.forEach((student) -> System.out.println(student.getName()));
}
public static class Student {
public Student(int id, String name, double cgpa) { this.id = id; this.name = name; this.cgpa = cgpa; } public int getId() { return id; } public String getName() { return name; } public double getCgpa() { return cgpa; } private int id; private String name; private double cgpa;
static class GPAComparator implements Comparator { public int compare(Student s1, Student s2) { int compareCGPA = Double.compare(s2.getCgpa(), s1.getCgpa()); int n = s1.getName().compareTo(s2.getName()); if (compareCGPA != 0) { return compareCGPA; } else if (n != 0) { return n; } else { return Double.compare(s1.getId(), s2.getId()); } } } }
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 →
import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Scanner;
public class Solution { public static void main(String[] args) {
}
public static class Student {
}
static class GPAComparator implements Comparator { public int compare(Student s1, Student s2) { int compareCGPA = Double.compare(s2.getCgpa(), s1.getCgpa()); int n = s1.getName().compareTo(s2.getName()); if (compareCGPA != 0) { return compareCGPA; } else if (n != 0) { return n; } else { return Double.compare(s1.getId(), s2.getId()); } } } }