#K10506. Sort Students by House and Name
Sort Students by House and Name
Sort Students by House and Name
You are given a list of student records where each record consists of a student's name and house. Your task is to sort these records primarily by the house name and, in case of ties, by the student's name.
The sorting order is defined as follows:
- Sort by house in lexicographical order.
- If two students belong to the same house, sort them by name in lexicographical order.
This can be formally written in \( \LaTeX \) as:
[ \text{key} = (\text{house}, \text{name}) ]
Input is given via standard input and output should be written to standard output. Each record should be printed on its own line with the student's name followed by a space and then the house.
inputFormat
The first line of input contains an integer n
representing the number of student records.
This is followed by n
lines, each containing two strings: the name
of the student and the house
they belong to, separated by a space.
Example:
5 Harry Gryffindor Hermione Gryffindor Ron Gryffindor Draco Slytherin Cedric Hufflepuff
outputFormat
Output the sorted student records, one record per line. Each line should contain the student's name and house separated by a space.
For the above example, the expected output should be:
Harry Gryffindor Hermione Gryffindor Ron Gryffindor Cedric Hufflepuff Draco Slytherin## sample
5
Harry Gryffindor
Hermione Gryffindor
Ron Gryffindor
Draco Slytherin
Cedric Hufflepuff
Harry Gryffindor
Hermione Gryffindor
Ron Gryffindor
Cedric Hufflepuff
Draco Slytherin
</p>