Gift Sorting Challenge

Sorting Algorithms Gifts Newyear

Problem Description

It's New Year time and you have prepared a list of special gifts for your colleagues. Each gift is represented as an object with two properties: name (a string) and price (a number). Your task is to write a function sortGifts that sorts these gift objects:

  1. First, by ascending order of price.
  2. If two gifts have the same price, then sort them alphabetically by name.

The function should return a new sorted array without modifying the original input.

Example

Given the following input:

[
  { "name": "Coffee Mug", "price": 15 },
  { "name": "Notebook", "price": 10 },
  { "name": "Pen", "price": 10 },
  { "name": "Desk Organizer", "price": 25 }
]

The sorted output should be:

[
  { "name": "Notebook", "price": 10 },
  { "name": "Pen", "price": 10 },
  { "name": "Coffee Mug", "price": 15 },
  { "name": "Desk Organizer", "price": 25 }
]

Requirements

  • Implement the function in a language-agnostic way. Avoid using language-specific shortcuts if possible.
  • Aim to complete the task in under 45 minutes.
  • Test your function with different cases, including when there are multiple gifts with the same price.

Happy New Year and good luck coding!