题解 | #成绩排序#
成绩排序
http://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int sNum = in.nextInt();
int type = in.nextInt();
List<Student> list = new ArrayList<>();
for (int i = 0; i < sNum; i++) {
list.add(new Student(in.next(), in.nextInt(), i));
}
if (type == 0){
// 分数高到低
list.stream()
.sorted(Comparator.comparing(Student::getScore).reversed().thenComparing(Student::getOrder))
.forEach(student -> System.out.println(student.getName() + " " + student.score));
}else {
list.stream()
.sorted(Comparator.comparing(Student::getScore).thenComparing(Student::getOrder))
.forEach(student -> System.out.println(student.getName() + " " + student.score));
}
}
}
class Student {
String name;
int score;
int order;
public Student(String name, int score, int order) {
this.name = name;
this.score = score;
this.order = order;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
}