#B3982. CSV Table Row Sorting

    ID: 11639 Type: Default 1000ms 256MiB

CSV Table Row Sorting

CSV Table Row Sorting

In the world of computing, a "table" is a simple, fundamental and widely-used data structure in databases, AI, and more. A table consists of rows and columns.

A common file format to represent tables is CSV (Comma-Separated Values). In this problem, you are given a simplified CSV file and a sorting specification. The CSV file adheres to the following rules:

  • The CSV file contains n lines of data, where the first line is the header.
  • Each line is a string corresponding to one row of the table. The cells in a row are separated by a half‐width comma (",").
  • Each cell has at least one character. There are two types of cells:
    • Numeric cell: Consists of at least one digit (0-9).
    • String cell: Consists of digits and letters (a-z, A-Z) and contains at least one letter.
  • All cells in the header row are string cells and are distinct.
  • For every other row, each column is either all numeric cells or all string cells.

You are also provided with a sorting specification string. For example, the specification "Score- , Name+ , p3-" means:

  • Sort primarily by the Score column in descending order (numeric order).
  • If there is a tie, sort by the Name column in ascending lexicographical order (string order).
  • If a tie still exists, sort by the p3 column in descending order (numeric order).

Input Format:

The input consists of multiple lines:

  1. The first line contains a positive integer n (n ≥ 2), representing the number of CSV lines (including header).
  2. The next n lines each contain a CSV string representing a row.
  3. The last line contains the sorting specification string. Each criterion is of the form ColumnName+ or ColumnName- (with an optional space around commas).

Output Format:

Output the CSV file after sorting the data rows (while keeping the header as the first line) according to the specified rules.

Note: You need to determine the cell type (numeric or string) based on the input data (all non-header cells in a column are of the same type).

inputFormat

The input contains multiple lines:

  1. An integer n (n ≥ 2) on the first line representing the number of CSV lines.
  2. The next n lines, each being a CSV string. The first of these is the header row.
  3. A final line with the sorting specification string, for example: Score- , Name+ , p3-.

outputFormat

Output all CSV lines after sorting the non-header rows according to the given specification. The header should remain at the top, and the rest of the lines should be reordered accordingly.

sample

4
Name,p1,p2,p3,Score
ZhangSan,40,30,28,98
LiSi,40,28,30,98
WangWu,40,25,20,85
Score- , Name+ , p3-
Name,p1,p2,p3,Score

LiSi,40,28,30,98 ZhangSan,40,30,28,98 WangWu,40,25,20,85

</p>