#C7556. Sort Products by Price
Sort Products by Price
Sort Products by Price
You are given a list of products, each having a name, a price, and a quantity. Your task is to sort the products in ascending order by price. In case of a tie (i.e. if two products have the same price), sort them in alphabetical order by name. The sorting key can be mathematically represented as \( (price, name) \).
The input is provided via standard input (stdin) and the final sorted list should be printed to standard output (stdout) in JSON format.
inputFormat
The first line of input contains an integer \( N \) representing the number of products. Each of the following \( N \) lines contains the details of a product in the format:
name price quantity
Here, name
is a string (without spaces), price
is a floating point number, and quantity
is an integer.
outputFormat
Output a single line: a JSON array of objects representing the sorted products. Each object must contain the keys name
, price
, and quantity
with their respective values.
4
apple 5.5 10
banana 3.0 5
kiwi 5.5 12
orange 4.0 8
[{"name": "banana", "price": 3.0, "quantity": 5}, {"name": "orange", "price": 4.0, "quantity": 8}, {"name": "apple", "price": 5.5, "quantity": 10}, {"name": "kiwi", "price": 5.5, "quantity": 12}]