Comparable

Lab Java Intermediate 20 points

Make Player(name, score) implement Comparable<Player> so sorting puts the highest score first, ties by name. Read players, sort with Arrays.sort, and print the ranking.

Input

n, then n lines name score.

Output

n lines RANK. NAME SCORE

Example

Input

4
zed 50
amy 90
bob 50
cat 70

Output

1. amy 90
2. cat 70
3. bob 50
4. zed 50
Sample input available Sample output available
Editor
Java

Hints

compareTo returns negative if this comes first.

Integer.compare(b, a) gives descending order.

Solution explanation

Comparable defines a class's natural ordering, used by Arrays.sort and TreeSet.

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

class Player implements Comparable<Player> {
    final String name;
    final int score;

    Player(String name, int score) {
        this.name = name;
        this.score = score;
    }

    @Override
    public int compareTo(Player other) {
        if (score != other.score) {
            return Integer.compare(other.score, score);
        }
        return name.compareTo(other.name);
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Player[] players = new Player[n];
        for (int i = 0; i < n; i++) {
            players[i] = new Player(sc.next(), sc.nextInt());
        }
        Arrays.sort(players);
        for (int i = 0; i < n; i++) {
            System.out.println((i + 1) + ". " + players[i].name + " " + players[i].score);
        }
    }
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD