You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.; import java.util.List; import java.util.ArrayList; class Student{ public String name; public int score; public Student(String name,int score){ this.name = name; this.score = score; }
} class Sortbyscore implements Comparator{ public int compare(Student a,Student b){ if(a.score == b.score){ return a.name.compareTo(b.name); } return b.score - a.score; } } public class Solution {
public static void main(String[] args) { ArrayList<Student> student = new ArrayList<Student>(); Scanner scan = new Scanner(System.in); int n = scan.nextInt(); for(int i=0;i<n;i++){ String names = scan.next(); int scores = scan.nextInt(); student.add(new Student(names,scores)); } Collections.sort(student,new Sortbyscore()); for(int i=0;i<student.size();i++){ System.out.println(student.get(i).name+" "+ student.get(i).score); } }
}
Seems like cookies are disabled on this browser, please enable them to open this website
Java Comparator
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.; import java.util.List; import java.util.ArrayList; class Student{ public String name; public int score; public Student(String name,int score){ this.name = name; this.score = score; }
} class Sortbyscore implements Comparator{ public int compare(Student a,Student b){ if(a.score == b.score){ return a.name.compareTo(b.name); } return b.score - a.score; } } public class Solution {
}