#C28. Sort Books

    ID: 46155 Type: Default 1000ms 256MiB

Sort Books

Sort Books

You are given a list of books. Each book has four properties: title, author, year_published, and genre. Your task is to sort these books in ascending order based on a given sort criterion. The criterion will be one of the following: title, author, year_published, or genre.

The sorting should follow these rules:

  • If the criterion is year_published, sort the books by their publication years (as integers) in ascending order.
  • For the other criteria (title, author, genre), sort based on lexicographical (alphabetical) order.

Input Format: The input is given from standard input (stdin) as follows:

  • The first line contains a string denoting the sort criterion.
  • The second line contains an integer n indicating the number of books.
  • Each of the following n lines contains the details of a book in the following format (fields separated by a semicolon):
    title;author;year_published;genre

Output Format: Print the sorted list of books to standard output (stdout). Each book should be printed in the same format as the input (i.e. title;author;year_published;genre), with one book per line.

Example:

Input:

title
3
B;Author1;2005;Genre2
A;Author2;2010;Genre1
C;Author3;2000;Genre3

Output:

A;Author2;2010;Genre1
B;Author1;2005;Genre2
C;Author3;2000;Genre3

The solution must handle all valid sort criteria and correctly sort the entries as described. In case of the year_published field, remember to compare numerically, while for other fields, compare as strings.

In mathematical terms, if we denote the list of books as \(B = [b_1, b_2, \dots, b_n]\) and let \(f(b)\) be the value corresponding to the chosen sort criterion, then the sorted list \(B' = [b'_1, b'_2, \dots, b'_n]\) must satisfy:

[ f(b'_1) \leq f(b'_2) \leq \dots \leq f(b'_n). ]

inputFormat

The input will be read from stdin and is formatted as follows:

  • The first line contains the sort criterion (one of title, author, year_published, or genre).
  • The second line contains an integer n, the number of books.
  • The next n lines each contain the description of a book, with its title, author, year_published, and genre separated by a semicolon.

outputFormat

The output should be printed to stdout and consist of n lines. Each line corresponds to a book record in the same format as the input (i.e., title;author;year_published;genre), sorted according to the given criterion in ascending order.

## sample
title
3
B;Author1;2005;Genre2
A;Author2;2010;Genre1
C;Author3;2000;Genre3
A;Author2;2010;Genre1

B;Author1;2005;Genre2 C;Author3;2000;Genre3

</p>