Dsa
Sorting
Hard
Library Book Inventory
You are maintaining an inventory of books in a library. Each book has a title, author, and a publication year. You need to sort this inventory: first by publication year in ascending order, then by title in alphabetical order if the years are the same. Each input line is formatted as 'title,author,year'. Output the sorted list, one per line, in the same format.
Example Input:
The Hobbit,J.R.R. Tolkien,1937
1984,George Orwell,1949
Brave New World,Aldous Huxley,1932
Moby Dick,Herman Melville,1851
Example Output:
Moby Dick,Herman Melville,1851
Brave New World,Aldous Huxley,1932
The Hobbit,J.R.R. Tolkien,1937
1984,George Orwell,1949
Key concepts
sortinglistskey
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