Sort by

recency

|

347 Discussions

|

  • + 0 comments

    Using Java 8

    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,
        Comparator.comparing(Student::getCgpa).reversed()
        .thenComparing(Student::getFname)
        .thenComparing(Student::getId));
    
        for(Student st: studentList){
            System.out.println(st.getFname());
        }
    }
    

    }

  • + 0 comments
    import java.util.*;
    
    class Student implements Comparable<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;
        }
    
        @Override
        public int compareTo(Student anotherStudent) {
            if (Double.compare(anotherStudent.getCgpa(), this.getCgpa()) != 0) {
                return Double.compare(anotherStudent.getCgpa(), this.getCgpa());
            } else if (!this.getFname().equals(anotherStudent.getFname())) {
                return this.getFname().compareTo(anotherStudent.getFname());
            } else {
                return this.getId() - anotherStudent.getId();
            }
        }
    }
    
    //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--;
            }
          
            in.close();
    
            Collections.sort(studentList);
          
              for (Student st: studentList) {
                System.out.println(st.getFname());
            }
        }
    }
    
  • + 0 comments
        Comparator<Student> cgpaComp = (s1, s2) -> Double.valueOf(s2.getCgpa()).compareTo(Double.valueOf(s1.getCgpa()));
        Comparator<Student> nameComp = (s1, s2) -> s1.getFname().compareTo(s2.getFname());
        Comparator<Student> idComp = (s1, s2) -> Integer.valueOf(s1.getId()).compareTo(Integer.valueOf(s2.getId()));
    
        studentList = studentList.
                stream().
                sorted(cgpaComp.thenComparing(nameComp).thenComparing(idComp)).
                collect(Collectors.toList());
    
  • + 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; } }

    //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--;
        }
        //String [] a=new String[testCases];
        for(int i=0;i<studentList.size()-1;i++){
            for(int j=0;j<studentList.size()-1;j++){
                if(studentList.get(j).getCgpa()<studentList.get(j+1).getCgpa()){
                    Student sl=studentList.get(j);
                    studentList.set(j, studentList.get(j+1));
                    studentList.set(j+1, sl);
                }
                else if(studentList.get(j).getCgpa()==studentList.get(j+1).getCgpa()){
                    if(studentList.get(j).getFname().compareTo(studentList.get(j+1).getFname())>0){
                        Student sl=studentList.get(j);
                        studentList.set(j, studentList.get(j+1));
                        studentList.set(j+1, sl);
                    }
                }
            }
        }
    
        for(Student st: studentList){
            System.out.println(st.getFname());
        }
    }
    

    }

  • + 0 comments

    Here is Java sort solution - https://programmingoneonone.com/hackerrank-java-sort-problem-solution.html