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.
- Prepare
- Java
- Data Structures
- Java Sort
- Discussions
Java Sort
Java Sort
Sort by
recency
|
336 Discussions
|
Please Login in order to post a comment
You are given a list of student information: ID, FirstName, and CGPA. Your task is to rearrange them according to their CGPA in decreasing order. If two student have the same CGPA, then arrange them according to their first name in alphabetical order. If those two students also have the same first name, then order them according to their ID. No two students have the same ID.
studentList.sort(new Comparator() {
import java.util.*;
// Define the Student class class Student { private int id; private String fname; private double cgpa;
}
// Implement the Comparator to sort the students class SortByCGPA implements Comparator { @Override public int compare(Student s1, Student s2) { // Compare CGPA first in decreasing order if (s2.getCgpa() != s1.getCgpa()) { return Double.compare(s2.getCgpa(), s1.getCgpa()); }
}
// Main solution class public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int testCases = Integer.parseInt(in.nextLine());
}
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()); } } } }