Hone logo
Hone
Problems

Custom Sorting for Product Inventory

In e-commerce and inventory management systems, it's common to need to sort products based on various criteria. Standard sorting algorithms often deal with simple data types, but when dealing with complex structures like product objects, you'll need to define custom sorting logic. This challenge will test your ability to implement custom sorting for a slice of product structs in Go.

Problem Description

You are tasked with building a system to sort a list of products. Each product has an ID, a Name, a Price, and a Stock Quantity. You need to implement a function that sorts a slice of these Product structs based on a provided sorting strategy. The sorting strategy can be any combination of the following criteria, applied in order of preference:

  1. Sort by Price (ascending): Products with lower prices should come first.
  2. Sort by Stock Quantity (descending): If prices are equal, products with higher stock quantities should come first.
  3. Sort by Product Name (alphabetical, ascending): If both price and stock quantity are equal, sort alphabetically by product name.

You will be given a slice of Product structs and will need to return a new slice that is sorted according to these rules.

Examples

Example 1:

Input:
Products: [
  {"ID": 101, "Name": "Laptop", "Price": 1200.00, "Stock": 50},
  {"ID": 102, "Name": "Mouse", "Price": 25.00, "Stock": 200},
  {"ID": 103, "Name": "Keyboard", "Price": 75.00, "Stock": 150}
]
Output:
[
  {"ID": 102, "Name": "Mouse", "Price": 25.00, "Stock": 200},
  {"ID": 103, "Name": "Keyboard", "Price": 75.00, "Stock": 150},
  {"ID": 101, "Name": "Laptop", "Price": 1200.00, "Stock": 50}
]
Explanation:
The products are sorted primarily by price in ascending order.

Example 2:

Input:
Products: [
  {"ID": 201, "Name": "T-Shirt", "Price": 20.00, "Stock": 300},
  {"ID": 202, "Name": "Jeans", "Price": 50.00, "Stock": 100},
  {"ID": 203, "Name": "Socks", "Price": 5.00, "Stock": 500},
  {"ID": 204, "Name": "Hat", "Price": 20.00, "Stock": 150}
]
Output:
[
  {"ID": 203, "Name": "Socks", "Price": 5.00, "Stock": 500},
  {"ID": 204, "Name": "Hat", "Price": 20.00, "Stock": 150},
  {"ID": 201, "Name": "T-Shirt", "Price": 20.00, "Stock": 300},
  {"ID": 202, "Name": "Jeans", "Price": 50.00, "Stock": 100}
]
Explanation:
"Socks" is the cheapest. "Hat" and "T-Shirt" have the same price. "T-Shirt" has more stock than "Hat", so "Hat" comes before "T-Shirt". "Jeans" is the most expensive.

Example 3:

Input:
Products: [
  {"ID": 301, "Name": "Apple", "Price": 1.00, "Stock": 100},
  {"ID": 302, "Name": "Banana", "Price": 1.00, "Stock": 100},
  {"ID": 303, "Name": "Cherry", "Price": 1.00, "Stock": 100}
]
Output:
[
  {"ID": 301, "Name": "Apple", "Price": 1.00, "Stock": 100},
  {"ID": 302, "Name": "Banana", "Price": 1.00, "Stock": 100},
  {"ID": 303, "Name": "Cherry", "Price": 1.00, "Stock": 100}
]
Explanation:
All products have the same price and stock. They are sorted alphabetically by name.

Constraints

  • The number of products in a slice will be between 0 and 1000.
  • Product IDs will be unique integers.
  • Product Names will be non-empty strings.
  • Prices will be non-negative floating-point numbers.
  • Stock Quantities will be non-negative integers.
  • The sorting logic must be stable.

Notes

You'll likely want to define a Product struct to represent the data. Go's standard library provides the sort package, which is highly flexible. Consider how you can adapt the sort.Interface to implement your custom sorting logic. Remember that the Less method in sort.Interface determines the order. You'll need to implement the comparison logic to satisfy all three sorting criteria.

Loading editor...
go