题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
import java.util.ArrayList; import java.util.List; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { static class Student { String name; int score; public Student(String name, int score) { this.name = name; this.score = score; } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = Integer.parseInt(in.nextLine()); int order = Integer.parseInt(in.nextLine()); List<Student> students = new ArrayList<>(); for (int i = 0; i < n; i++) { String[] s = in.nextLine().split(" "); Student student = new Student(s[0], Integer.parseInt(s[1])); students.add(student); } if(order == 0) { students.sort((a, b) -> b.score - a.score);//升序 } else { students.sort((a, b) -> a.score - b.score);//降序 } for (Student student : students) { System.out.println(student.name + " " + student.score); } } }