You are viewing a single comment's thread. Return to all comments →
The code below doesn't sort all names properly. In the sample test case, it interchanges Maria and Shafaet's names. Please tell me where the error is.
class Student { private int id; private String name; private double cgpa; Student(int id, String name, double cgpa) { super(); this.id=id; this.name=name; this.cgpa=cgpa; } public int getID() { return this.id; } public String getName() { return this.name; } public double getCGPA() { return this.cgpa; } } class Priorities { public List<Student> getStudents(List<String> events) { PriorityQueue<Student> pq=new PriorityQueue<Student>(100,new Comparator<Student>(){ public int compare(Student o1, Student o2) { if (o1.getCGPA() == o2.getCGPA()) { if (o1.getName().equals(o2.getName())) { return o1.getID() - o2.getID(); } else { return o1.getName().compareTo(o2.getName()); } } else { return o2.getCGPA() > o1.getCGPA() ? 1 : -1; } } }); for(String e:events) { String event[]=e.split(" "); Scanner scan=new Scanner(System.in); if(event[0].equals("ENTER")) { String name=event[1]; double cg=Double.parseDouble(event[2]); int id=Integer.parseInt(event[3]); Student st=new Student(id,name,cg); pq.add(st); } else if(event[0].equals("SERVED")) pq.poll(); } return new ArrayList<Student>(pq); } }
Java Priority Queue
You are viewing a single comment's thread. Return to all comments →
The code below doesn't sort all names properly. In the sample test case, it interchanges Maria and Shafaet's names. Please tell me where the error is.