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
|
333 Discussions
|
Please Login in order to post a comment
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()); } } } }
//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());
}
// 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()); // }
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){
}