import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ solution1(in); // solution2(in); // solution3(in); // solution4(in); // solution5(in); } } /** * List.sort() - lambda * @param in */ private static void solution1(Scanner in){ int N = in.nextInt(); int sort = in.nextInt(); List<Student> studentList = new ArrayList<>(); for(int i=1; i<=N; i++){ String name = in.next(); Integer score = in.nextInt(); studentList.add(new Student(name, score)); } // desc - default stable if(sort == 0){ studentList.sort((o1, o2) -> o2.getScore() - o1.getScore()); } // asc - default stable else if(sort == 1){ studentList.sort((o1, o2) -> o1.getScore() - o2.getScore()); } for(Student student: studentList){ System.out.println(student.toString()); } } /** * List.sort() - Comparator.comparingInt() * @param in */ private static void solution2(Scanner in){ int N = in.nextInt(); int sort = in.nextInt(); List<Student> studentList = new ArrayList<>(); for(int i=1; i<=N; i++){ String name = in.next(); Integer score = in.nextInt(); studentList.add(new Student(name, score)); } // desc - default stable if(sort == 0){ studentList.sort(Comparator.comparingInt(Student::getScore).reversed()); } // asc - default stable else if(sort == 1){ studentList.sort(Comparator.comparingInt(Student::getScore)); } for(Student student: studentList){ System.out.println(student.toString()); } } /** * List.sort() - List.stream().sorted(Comparator.comparing()).collect() * @param in */ private static void solution3(Scanner in){ int N = in.nextInt(); int sort = in.nextInt(); List<Student> studentList = new ArrayList<>(); for(int i=1; i<=N; i++){ String name = in.next(); Integer score = in.nextInt(); studentList.add(new Student(name, score)); } // desc - default stable if(sort == 0){ studentList = studentList.stream().sorted(Comparator.comparing(Student::getScore).reversed()).collect(Collectors.toList()); } // asc - default stable else if(sort == 1){ studentList = studentList.stream().sorted(Comparator.comparing(Student::getScore)).collect(Collectors.toList()); } for(Student student: studentList){ System.out.println(student.toString()); } } /** * List.sort() - Comparator<Student> * @param in */ private static void solution4(Scanner in){ int N = in.nextInt(); int sort = in.nextInt(); List<Student> studentList = new ArrayList<>(); for(int i=1; i<=N; i++){ String name = in.next(); Integer score = in.nextInt(); studentList.add(new Student(name, score)); } // desc - default stable if(sort == 0){ studentList.sort(new Comparator<Student>(){ @Override public int compare(Student o1, Student o2){ return o2.getScore() - o1.getScore(); } }); } // asc - default stable else if(sort == 1){ studentList.sort(new Comparator<Student>(){ @Override public int compare(Student o1, Student o2){ return o1.getScore() - o2.getScore(); } }); } for(Student student: studentList){ System.out.println(student.toString()); } } /** * List.sort() - Student.INCREASE & Student.DECREASE * @param in */ private static void solution5(Scanner in){ int N = in.nextInt(); int sort = in.nextInt(); List<Student> studentList = new ArrayList<>(); for(int i=1; i<=N; i++){ String name = in.next(); Integer score = in.nextInt(); studentList.add(new Student(name, score)); } // desc - default stable if(sort == 0){ studentList.sort(Student.DECREASE); } // asc - default stable else if(sort == 1){ studentList.sort(Student.INCREASE); } for(Student student: studentList){ System.out.println(student.toString()); } } private static class Student { private String name; private Integer score; private static final Comparator<Student> INCREASE = new increase(); private static final Comparator<Student> DECREASE = new decrease(); public String getName(){ return name; } public Integer getScore(){ return score; } public Student(String name, Integer score){ this.name = name; this.score = score; } public static class increase implements Comparator<Student>{ @Override public int compare(Student o1, Student o2){ return o1.getScore() - o2.getScore(); } } public static class decrease implements Comparator<Student>{ @Override public int compare(Student o1, Student o2){ return o2.getScore() - o1.getScore(); } } @Override public String toString(){ return name + " " + score; } } }