1.根据题目要求通过nextInt分别获取需要排序的成绩个数和排序方式。
in.nextInt。
2.调用一次nextLine将我们的多余空格行去除。
in.nextLine
3.通过成绩个数遍历,向List中添加需要排序的学生信息。
List<String> students = new ArrayList<String>(); while (sortCount-- > 0) { students.add(in.nextLine()); }
4.在对list列表进行排序。
if (sortMethod == 0) { students.sort(Comparator.comparing((String student)->Integer.valueOf(student.split(" ")[1])).reversed()); } else { students.sort(Comparator.comparing(( student)->Integer.valueOf( student.split(" ")[1]))); }
5.获取排好序的列表直接通过println输出即可
students.forEach(System.out::println);
tips:
Comparator.comparing中Lambda表达式需要参数添加类型,避免reversed推断类型报错。