1 题目
2 代码
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class A1028 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
int N = Integer.parseInt(input[0]);
int C = Integer.parseInt(input[1]);
ArrayList<StudentRecord> studentRecords = new ArrayList<>();
for (int i = 0; i < N; i++) {
String[] line = br.readLine().split(" ");
studentRecords.add(new StudentRecord(line[0], line[1], Integer.parseInt(line[2])));
}
switch (C) {
case 1:
Collections.sort(studentRecords, new Comparator<StudentRecord>() {
@Override
public int compare(StudentRecord o1, StudentRecord o2) {
return o1.getId().compareTo(o2.getId());
}
});
break;
case 2:
Collections.sort(studentRecords, new Comparator<StudentRecord>() {
@Override
public int compare(StudentRecord o1, StudentRecord o2) {
if (!o1.getName().equals(o2.getName()))
return o1.getName().compareTo(o2.getName());
else
return o1.getId().compareTo(o2.getId());
}
});
break;
case 3:
Collections.sort(studentRecords, new Comparator<StudentRecord>() {
@Override
public int compare(StudentRecord o1, StudentRecord o2) {
if (o1.getGrade() != o2.getGrade())
return o1.getGrade() - o2.getGrade();
else
return o1.getId().compareTo(o2.getId());
}
});
break;
}
for (StudentRecord s : studentRecords) {
System.out.println(s.getId() + " " + s.getName() + " " + s.getGrade());
}
}
}
class StudentRecord {
private String id;
private String name;
private int grade;
public StudentRecord(String id, String name, int grade) {
this.id = id;
this.name = name;
this.grade = grade;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
3 要点
(1)需要按特定列排序,列数较少也没必要找规律,直接Switch Case语句即可。