Hone logo
Hone
Problems

Implementing a Generic Sort Interface in Go

In Go, sorting collections is a common task. To make sorting flexible and reusable across different data types, Go provides the sort package, which relies on interfaces. This challenge will test your understanding of Go interfaces and how to leverage them for generic sorting.

Problem Description

Your task is to implement the sort.Interface interface for a custom data type. This interface requires you to define three methods: Len(), Less(i, j int), and Swap(i, j int). By implementing these methods, you'll enable the standard sort.Sort() function to sort instances of your custom type.

You will be given a slice of a custom struct type. You need to make this slice sortable using the sort.Sort() function. The sorting should be in ascending order based on a specific field of your struct.

Key Requirements:

  1. Define a custom struct type.
  2. Create a slice of your custom struct.
  3. Implement the sort.Interface for a type that is a slice of your custom struct.
    • Len() should return the number of elements in the slice.
    • Less(i, j int) should return true if the element at index i should come before the element at index j in sorted order. This comparison should be based on a specific field of your struct.
    • Swap(i, j int) should swap the elements at indices i and j in the slice.
  4. Use sort.Sort() to sort the slice.

Expected Behavior:

After calling sort.Sort() on a slice of your custom struct that implements sort.Interface, the slice should be sorted in ascending order based on the specified field.

Edge Cases:

  • An empty slice should be handled gracefully (sorting should have no effect).
  • A slice with a single element is already sorted.

Examples

Example 1:

Input: A slice of Person structs, where Person has Name (string) and Age (int) fields. The slice is: [{Name: "Alice", Age: 30}, {Name: "Bob", Age: 25}, {Name: "Charlie", Age: 35}] We want to sort by Age in ascending order.

Output: [{Name: "Bob", Age: 25}, {Name: "Alice", Age: 30}, {Name: "Charlie", Age: 35}]

Explanation: The slice is sorted such that the person with the youngest age appears first, followed by the next youngest, and so on.

Example 2:

Input: A slice of Product structs, where Product has ID (int) and Price (float64) fields. The slice is: [{ID: 101, Price: 19.99}, {ID: 103, Price: 15.50}, {ID: 102, Price: 25.00}, {ID: 104, Price: 15.50}] We want to sort by Price in ascending order. If prices are equal, maintain their relative order (stable sort is not explicitly required, but a typical implementation of sort.Sort will handle this).

Output: [{ID: 103, Price: 15.50}, {ID: 104, Price: 15.50}, {ID: 101, Price: 19.99}, {ID: 102, Price: 25.00}]

Explanation: The products are sorted primarily by their price from lowest to highest.

Example 3:

Input: An empty slice of Book structs, where Book has Title (string) and Pages (int) fields.

Output: []

Explanation: Sorting an empty slice results in an empty slice.

Constraints

  • The custom struct will have at least two fields, one of which will be a comparable type (e.g., int, string, float64).
  • You must implement the sort.Interface directly for a type that is a slice of your custom struct. You should not use sort.Slice or sort.SliceStable.
  • The sorting should be in ascending order based on the specified field.
  • The input will always be a slice of your custom struct.

Notes

  • Recall that the sort.Interface is a standard Go interface. You'll need to define a type that holds your slice and then add the three required methods (Len, Less, Swap) to that type.
  • Consider how you will choose which field to sort by. You might want to define your custom slice type and then decide the sorting logic within the Less method.
  • The sort.Sort() function takes an argument of type sort.Interface. Your custom slice type will need to satisfy this interface.
Loading editor...
go