Holiday Gift Sorter

Sorting Algorithms Holiday Custom Sort

Problem Description

On November 3rd, as we prepare to celebrate the upcoming holiday season, you are tasked with a special sorting challenge. Imagine you have a list of holiday gifts. Each gift is represented as an object with the following properties:

  • name (string): The name of the gift.
  • weight (number): The weight of the gift.
  • cost (number): The cost of the gift.

Your task is to write a function sortGifts that sorts the array of gifts with the following criteria:

  1. Sort by weight in ascending order.
  2. If two gifts have the same weight, sort them by cost in descending order.

Input

An array of gift objects. For example:

[
  { "name": "Toy Car", "weight": 2, "cost": 20 },
  { "name": "Doll", "weight": 1, "cost": 15 },
  { "name": "Board Game", "weight": 2, "cost": 25 }
]

Output

A sorted array of gift objects meeting the criteria above.

Example

Given the input above, your function should return:

[
  { "name": "Doll", "weight": 1, "cost": 15 },
  { "name": "Board Game", "weight": 2, "cost": 25 },
  { "name": "Toy Car", "weight": 2, "cost": 20 }
]

Starter Code

Below you will find the language-agnostic starter code stubs for your implementation.

Happy coding and enjoy the holiday spirit!