#C14635. Sort Products by Price and Name

    ID: 44306 Type: Default 1000ms 256MiB

Sort Products by Price and Name

Sort Products by Price and Name

You are given a JSON array representing a list of products. Each product is a JSON object with two keys: (\texttt{name}) (a string) and (\texttt{price}) (a floating-point number). Your task is to sort the products in increasing order by their price. If two products have the same price, they should be sorted in lexicographical (alphabetical) order based on their name.

The input is read from standard input (stdin) as a single JSON array. The output should be the sorted list in JSON format written to standard output (stdout).

Formally, if the input products are (P = [p_1, p_2, \dots, p_n]) where each product (p_i = {\texttt{name}: s_i, \texttt{price}: r_i}), then you must output a permutation (P') of (P) such that for any two products (p_i) and (p_j) in (P'), either (r_i < r_j) or (r_i = r_j) and (s_i \leq s_j) in lexicographical order.

inputFormat

The input consists of a single line containing a JSON array of products. Each product is an object with two keys:

  • name: a string representing the product name.
  • price: a number representing the product price.

outputFormat

Output a JSON array representing the sorted list of products. The sorting should be done in ascending order by price, and if two products have the same price, by name in alphabetical order. The output must be printed to standard output (stdout).## sample

[{"name": "Pencil", "price": 1.5}, {"name": "Pen", "price": 1.5}, {"name": "Notebook", "price": 2.5}, {"name": "Eraser", "price": 0.5}]
[{"name":"Eraser","price":0.5},{"name":"Pen","price":1.5},{"name":"Pencil","price":1.5},{"name":"Notebook","price":2.5}]