api调用,就不用自己排咯
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int order = scanner.nextInt();
ArrayList<Stu> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
String name = scanner.next();
int score = scanner.nextInt();
Stu stu = new Stu(name, score, order);
list.add(stu);
}
Collections.sort(list, new Comparator<Stu>() {
@Override
public int compare(Stu o1, Stu o2) {
if (o1.getOrder() == 1) {
// 升序
return o1.getScore() - o2.getScore();
} else {
// 降序
return o2.getScore() - o1.getScore();
}
}
});
for (Stu stu : list) {
System.out.println(stu.getName() + " " + stu.getScore());
}
}
}
class Stu {
public Stu(String name, int score, int order) {
this.name = name;
this.score = score;
this.order = order;
}
private String name;
private int score;
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
private int 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;
}
}
SHEIN希音公司福利 284人发布
查看8道真题和解析