题解 | #成绩排序# 定义一个类存储成绩
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
import java.util.Scanner; import java.util.Arrays; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int sort_order = input.nextInt(); Student[] students = new Student[n]; for (int i = 0; i < n; i++) { String name = input.next(); int score = input.nextInt(); students[i] = new Student(name, score, i); } // 排序 Arrays.sort(students,(o1,o2)->{ if(sort_order==0){ return o2.score-o1.score; } else{ return o1.score-o2.score; } }); // 处理成绩相同的情况 for (int i = 0; i < n; i++) { System.out.println(students[i].name + " " + students[i].score); } } static class Student { String name; int score; int index; Student(String name, int score, int index) { this.name = name; this.score = score; this.index = index; } } }