Sort by

recency

|

333 Discussions

|

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

  • + 0 comments

    //here i am giving the solution of a code import java.util.*;

    class Student{ private int id; private String fname; private double cgpa; public Student(int id, String fname, double cgpa) { super(); this.id = id; this.fname = fname; this.cgpa = cgpa; } public int getId() { return id; } public String getFname() { return fname; } public double getCgpa() { return cgpa; } }

    //Complete the code 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<Student>();
        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--;
        }
      Collections.sort(studentList, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                // Compare by CGPA (descending order)
                if (Double.compare(s2.getCgpa(), s1.getCgpa()) != 0) {
                    return Double.compare(s2.getCgpa(), s1.getCgpa());
                }
                // Compare by first name (alphabetical order)
                if (!s1.getFname().equals(s2.getFname())) {
                    return s1.getFname().compareTo(s2.getFname());
                }
                // Compare by ID (ascending order)
                return Integer.compare(s1.getId(), s2.getId());
            }
        });
        for(Student st: studentList){
            System.out.println(st.getFname());
        }
    }
    

    }

  • + 0 comments

    // by defining user defined comparator we can solve it. it's same as others solved it but passing as argument in streams using lmbda functions.

    studentList.stream().sorted((student1,student2)->{int compareCGPA = Double.compare(student2.getCgpa(), student1.getCgpa()); if (compareCGPA != 0) { return compareCGPA; } else { return student1.getFname().compareTo(student2.getFname()); }}).forEach(i->System.out.println(i.getFname())); // for(Student st: studentList){ // System.out.println(st.getFname()); // }

  • + 0 comments

    import java.util.*;

    class Student{ private int id; private String fname; private double cgpa; public Student(int id, String fname, double cgpa) { super(); this.id = id; this.fname = fname; this.cgpa = cgpa; } public int getId() { return id; } public String getFname() { return fname; } public double getCgpa() { return cgpa; } }

    public class Solution { public static void main(String[] args){

    Comparator<Student> stu = new Comparator<Student>() 
    {
      public int compare(Student s1,Student s2)
       {
           if (Double.compare(s2.getCgpa(),s1.getCgpa())!=0) 
           {
               return Double.compare(s2.getCgpa(),s1.getCgpa());
           }
           else if (!s1.getFname().equals(s2.getFname())) 
           {
               return s1.getFname().compareTo(s2.getFname());
           }
           else
           {
               return Integer.compare(s1.getId(), s2.getId());
           }
       }
    };
    
        Scanner in = new Scanner(System.in);
        int testCases = Integer.parseInt(in.nextLine());
    
        List<Student> studentList = new ArrayList<Student>();
        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--;
        }
    
         Collections.sort(studentList, stu);
    
        for(Student st: studentList){
            System.out.println(st.getFname());
        }
    }
    

    }