Go Reference ยท Collections: sort, slices, maps
Go sort.Interface type
Reference for Go sort.Interface: implement Len, Less and Swap to sort custom collections with sort.Sort and sort.Stable, embedding tricks and sort.Reverse.
sort.Interface is the interface behind sort.Sort, sort.Stable, sort.IsSorted and sort.Reverse. A type satisfies it by providing three methods: how many elements there are, whether one element sorts before another, and how to swap two elements. That lets you sort things that are not plain slices, or give a slice type a fixed, reusable ordering.
Before generics and sort.Slice this was the only way to sort custom types, so you still meet it in older code. Today it is most useful when the sort order should travel with a named type.
type Interface interface { Len() int Less(i, j int) bool Swap(i, j int) }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
type Interface interface { Len() int Less(i, j int) bool Swap(i, j int) } func Sort(data Interface) func Stable(data Interface) func IsSorted(data Interface) bool func Reverse(data Interface) Interface
| Method | Description |
|---|---|
Len() |
The number of elements. |
Less(i, j) |
Reports whether element i must sort before element j. Must be a strict weak ordering (use <, not <=). |
Swap(i, j) |
Exchanges elements i and j. |
Returns: Sort and Stable sort in place and return nothing; IsSorted reports sortedness; Reverse returns a wrapper whose Less is the opposite, so sorting it gives decreasing order.
Usage notes
- Convert, then sort. A named slice type with the three methods is sorted by converting:
sort.Sort(ByAge(people)). The conversion costs nothing, since both types share the same memory. Sortis not stable. Usesort.Stablewhen equal elements must keep their relative order.- The methods use value receivers on a slice type, which is fine because
Swapchanges the shared underlying array, not the slice header. - Embedding trick: keep
LenandSwapon one base type and embed it in small structs that add onlyLess, as in the second example. Each new ordering is then three lines. - Descending order: wrap with
sort.Reverse(ByAge(x))instead of writing a second type. - Beyond slices:
Interfacecan sort a linked structure or parallel slices (swap both slices inSwap) because it only talks in indexes. - Prefer
sort.Sliceorslices.SortFuncfor one-off orderings; they need no extra type. Reach forInterfacewhen the ordering is reused or when the data is not a single slice. - Interface calls are slower than
slices.SortFunc, which is generic and avoids dynamic dispatch for each comparison.
Related
sort,sort.Slice(),slices.SortFunc()- Lessons: Sorting, Interfaces