Challenge: Rank the Students

Lab Java Intermediate 20 points

Read n students, each a name and marks. Print them from highest marks to lowest. If two have the same marks, put them in alphabetical order by name.

Input

First n, then n lines: name and marks.

Output

One line per student: <name> <marks>.

Example

Input

4
Ravi 78
Asha 91
Meera 78
Zoya 85

Output

Asha 91
Zoya 85
Meera 78
Ravi 78
Sample input available Sample output available
Editor
Java

Hints

Store Student objects in an ArrayList.

In the comparator: if the marks differ, return b.marks - a.marks; otherwise compare the names.

Solution explanation

b.marks - a.marks sorts high to low; when that is 0, compareTo on the names breaks the tie.

Show solution
import java.util.ArrayList;
import java.util.Scanner;

class Student {
    String name;
    int marks;
    Student(String name, int marks) { this.name = name; this.marks = marks; }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        ArrayList<Student> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(new Student(sc.next(), sc.nextInt()));
        }
        list.sort((a, b) -> a.marks != b.marks ? b.marks - a.marks : a.name.compareTo(b.name));
        for (Student s : list) {
            System.out.println(s.name + " " + s.marks);
        }
    }
}
Output
Answer
HOME LABS COMMUNITY DASHBOARD