Sort Records
Completed
Read n, then n lines name age. Print the people sorted by age, and by name when ages are equal, as name (age).
Input
Line 1: n. Then n lines.
Output
n lines.
Example
Input
3 zoe 25 adam 30 bob 25
Output
bob (25) zoe (25) adam (30)
Editor
Python
Hints
Tuples sort by their first item, then the second, and so on.
Store (age, name) so sorting does the work.
Solution explanation
Putting the main sort key first in the tuple gives multi-level sorting for free.
Show solution
n = int(input())
people = []
for _ in range(n):
name, age = input().split()
people.append((int(age), name))
for age, name in sorted(people):
print(f"{name} ({age})")