liveBook · Manning

Sort.Interface

sort.Interface is a Go interface requiring Len, Less, and Swap methods for sorting a collection.

How 2 Manning books explain it

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

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.