Dsa
Sorting
Medium
Library Book Sorting
You are designing a library system where books need to be sorted for a catalog. Each book has a title and an author. You need to sort the books first by author name (ascending) and then by book title (ascending) if the author names are the same. Your program should read a list of books and their authors, and output the sorted list of book titles.
Input:
The first line contains an integer N (1 <= N <= 1000), the number of books.
The next N lines each contain a book title (a string) and an author name (a string).
Output:
Print the sorted list of book titles, one per line.
Example:
Input:
3
WarAndPeace LeoTolstoy
1984 GeorgeOrwell
AnimalFarm GeorgeOrwell
Output:
1984
AnimalFarm
WarAndPeace
Key concepts
sortingstringsbooks
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now