The sort.Interface Interface
Let’s learn about the sort.Interface interface’s functionality.
We'll cover the following...
We'll cover the following...
Definition of sort.Interface
The sort package contains an interface named sort.Interface that allows us to sort slices according to our needs and our data, provided that we implement sort.Interface for the custom data types stored in our slices. The sort package defines the sort.Interface as follows:
type Interface interface {// Len is the number of elements in the collection.Len() int// Less reports whether the element with// index i should sort before the element with index j.Less(i, j int) bool// Swap swaps the elements with indexes i and j.Swap(i, j int)}
Implementing sort.Interface
What we can understand from the definition of sort.Interface is that in order to implement sort.Interface, we need to implement the following three type methods: