题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
import java.util.*; public class Main { public static int ORDER = 1; public static void main(String[] args) { Scanner in = new Scanner(System.in); Integer count = Integer.parseInt(in.nextLine()); Integer orderInput = Integer.parseInt(in.nextLine()); if(orderInput.intValue() == 0) ORDER = -1; Set<Student> set = new TreeSet<Student>(); while (count-- > 0) { String[] studentData = in.nextLine().split(" "); set.add(new Student(studentData[0], studentData[1])); } set.forEach(st -> System.out.println(st)); } } class Student implements Comparable<Student> { String name; int score; public Student() {} public Student(String name, int score) { this.name = name; this.score = score; } public Student(String name, String score) { this.name = name; try { this.score = Integer.parseInt(score); } catch (Exception e) { this.score = 0; } } public String getName() { return this.name; } public int getScore() { return this.score; } public void setName(String name) { this.name = name; } public void setScore(int score) { this.score = score; } @Override public int compareTo(Student o) { return (this.score - o.getScore()) * Main.ORDER; } @Override public String toString(){ return this.name + " " + this.score; } }