How it works...
The sample code shows both ways of how to comfortably sort a slice with the help of the sort package. The first approach is more ad hoc and it uses the Slice function of the sort package. The Slice function consumes the slice to be sorted and the so-called less function, which defines whether the element i should be sorted before element j.
The second approach requires more code and planning ahead. It leverages the Interface interface of the sort package. The interface acts as a representative of the data and requires it to implement essential methods on sorted data: Len (defines the amount of data), Less (less function), Swap (method called to swap the elements). If the data value implements this interface, then the Sort ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access