Sort by

recency

|

328 Discussions

|

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

    class SortStudent implements Comparator{ @Override public int compare(Student s1, Student s2){ int cgpaCompare = Double.compare(s2.getCgpa(), s1.getCgpa()); int namecompare = s1.getFname().compareTo(s2.getFname()); int compareId = s1.getId() - s2.getId();

        if(cgpaCompare ==0 && namecompare == 0){
            return compareId;
        } else
        if(cgpaCompare==0){
            return namecompare;
        } else {
            return cgpaCompare;
        }
    
    }
    

    } //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 SortStudent());
       studentList.sort(new SortStudent());
        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<Student> {
        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
    //Complete the code
    //implemented the solution using Comparator 
    class GPAComparator implements Comparator<Student>{
        public int compare(Student s1, Student s2) {
            int compareCGPA = Double.compare(s2.getCgpa(), s1.getCgpa()); 
            if (compareCGPA != 0) {
                return compareCGPA;
            } else {
                return s1.getFname().compareTo(s2.getFname());
            }
        }
    }
    
    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--;
    		}
    				//this line of solution is required to compare studentList
            Collections.sort(studentList, new GPAComparator());
            
          	for(Student st: studentList){
    			System.out.println(st.getFname());
    		}
    	}
    
  • + 0 comments
            Collections.sort(studentList,new Comparator<Student>(){
                    public int compare(Student s1,Student s2){
                    int[] res = {Double.compare(s2.getCgpa(),
                    s1.getCgpa()),s1.getFname().compareTo(s2.getFname()),
                    Integer.compare(s1.getId(), s2.getId())};
                    for (int r : res){if (r != 0){return r;}}
                    return 0 ; 
                    }
            });
    
  • + 0 comments

    import java.util.*;

    class Student implements Comparable{ 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; } //= code writen here= public int compareTo(Student s){ if(this.getCgpa()

    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);
    
        for(Student st: studentList){
            System.out.println(st.getFname());
        }
    }
    

    }