#K64132. Sorting Cars by Criteria

    ID: 31908 Type: Default 1000ms 256MiB

Sorting Cars by Criteria

Sorting Cars by Criteria

You are given a list of car models. Each car model is characterized by its name, fuel efficiency, and engine size. The task is to sort the list of cars based on two criteria: a primary criterion and a secondary criterion. The sorting order can be either ascending (asc) or descending (desc).

The sorting follows the rule: first, the cars are sorted according to the primary criterion. If two cars are equal in the primary criterion, the secondary criterion is used to break the tie.

The criteria can be one of the following: name, fuel_efficiency, or engine_size. In terms of sorting, consider the mathematical rule for stability: if we denote the key extraction as \( f(\text{car}) \), then the sorted order satisfies \[ f(\text{car}_i) \leq f(\text{car}_{i+1}) \quad \text{(for ascending order)}. \]

Implement the sorting in a way that reads input from standard input and writes the sorted car models to standard output.

inputFormat

The input is given via standard input (stdin) and has the following format:

n
car_1_name car_1_fuel_efficiency car_1_engine_size
car_2_name car_2_fuel_efficiency car_2_engine_size
... (n lines in total)
primary
secondary
order

Here:

  • n is an integer representing the number of car models.
  • Each of the next n lines contains the details of a car model: the model name (a string), fuel efficiency (a number), and engine size (a number), separated by spaces.
  • primary is the primary sorting criterion.
  • secondary is the secondary sorting criterion.
  • order is a string which is either asc for ascending order or desc for descending order.

outputFormat

The output should be printed to standard output (stdout) with each line representing a sorted car model. The format for each line should be:

car_name fuel_efficiency engine_size

Each value is separated by a single space. Ensure that numbers are printed in a human‐readable format.

## sample
4
supra 15.5 3.0
modelx 20.0 4.5
civic 30.1 1.5
mustang 18.3 5.0
name
fuel_efficiency
asc
civic 30.1 1.5

modelx 20.0 4.5 mustang 18.3 5.0 supra 15.5 3.0

</p>