Sort.Interface
sort.Interface is a Go interface requiring Len, Less, and Swap methods for sorting a collection.
Learn Go with Pocket-Sized Projects
sort.Interface is a Go interface requiring Len, Less, and Swap methods so a collection can be sorted by the sort package.
- How it is used
- Uses a custom collection type to provide reusable sorting by author and title through sort.Sort.
- How deep it goes
- Defines all three methods and demonstrates chained comparison, but does not detail the sorting algorithm behind sort.Sort.
- Examples
- Demonstrates sorting a Book slice by Author and then Title with a named byAuthor collection type.
- What you can do after
- Apply it via sort.Sort to reorder a slice using custom collection behavior and multi-field comparisons.
Read how Learn Go with Pocket-Sized Projects explains sort.Interface →
100 Go Mistakes and How to Avoid Them
The Go sorting interface that defines collection length, element ordering, and element-swapping operations.
- How it is used
- Abstracts index-based collection sorting so callers depend on behavior rather than concrete collection types or sorting algorithms.
- How deep it goes
- Defines three required operations and explains reuse, while stopping short of implementing a sorting algorithm.
- Examples
- Uses Len, Less, and Swap implementations alongside sort.IsSorted as a consumer of the interface.
- What you can do after
- Apply it via the sort package to support interchangeable collection implementations and sortedness checks.
Read how 100 Go Mistakes and How to Avoid Them explains sort.Interface →
Related concepts
- contractkind of · An API contract is a specification of the operations, data, and behavioral expectations that govern interactions between software components.
- sort packagepart of · sort is Go’s standard-library package for sorting slices and other collections.
- sort.Slicealternative to · sort.Slice is a Go function that sorts a slice in place according to a supplied less-than comparison function.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

