Sort by

recency

|

336 Discussions

|

  • + 0 comments

    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.

  • + 0 comments
    In TestCase1, why do these entries appear in this order when the GPA is the same (2.01) and "HYasEeNIkTsDSnuMXQGDuFIZuGen" comes alphabetically after "euKDKBZrwqODRtIyWji"?
    
    HYasEeNIkTsDSnuMXQGDuFIZuGen euKDKBZrwqODRtIyWji
    
    Here's an enhanced output I generated for debugging purposes of my own code. As you can see, my code reverses the order, which I believe to be correct, unless I'm overlooking something:
    
    euKDKBZrwqODRtIyWji 2.01 74 HYasEeNIkTsDSnuMXQGDuFIZuGen 2.01 8
    
    Any insight would be appreciated, thank you!
    
  • + 0 comments

    studentList.sort(new Comparator() {

            @Override
            public int compare(Student o1, Student o2) {
                int compareName =o1.getFname().compareTo(o2.getFname());
                int compareCgpa=Double.compare(o2.getCgpa(), o1.getCgpa());
                int compareId=Integer.compare(o1.getId(), o2.getId());        
    
                return  (compareCgpa==0)?(compareName==0)?compareId:compareName:compareCgpa;
            }
    
  • + 0 comments

    import java.util.*;

    // Define the Student class class Student { private int id; private String fname; private double cgpa;

    public Student(int id, String fname, double cgpa) {
        this.id = id;
        this.fname = fname;
        this.cgpa = cgpa;
    }
    
    public int getId() {
        return id;
    }
    
    public String getFname() {
        return fname;
    }
    
    public double getCgpa() {
        return 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()); }

        // If CGPA is same, compare by first name in alphabetical order
        int nameComparison = s1.getFname().compareTo(s2.getFname());
        if (nameComparison != 0) {
            return nameComparison;
        }
    
        // If both CGPA and first name are the same, compare by ID in increasing order
        return Integer.compare(s1.getId(), s2.getId());
    }
    

    }

    // Main solution class public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int testCases = Integer.parseInt(in.nextLine());

        List<Student> studentList = new ArrayList<>();
    
        // Input data
        while (testCases > 0) {
            int id = in.nextInt();
            String fname = in.next();
            double cgpa = in.nextDouble();
    
            Student st = new Student(id, fname, cgpa);
            studentList.add(st);
    
            testCases--;
        }
    
        // Sort the list using the custom comparator
        Collections.sort(studentList, new SortByCGPA());
    
        // Output the first name of each student
        for (Student st : studentList) {
            System.out.println(st.getFname());
        }
    }
    

    }

  • + 0 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()); } } } }