Skip to content
Tutorials Center
My learning

Topics

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.

Go
type Interface interface {
	Len() int
	Less(i, j int) bool
	Swap(i, j int)
}

Try it

Go A named slice type that sorts itself

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.

Go Several orderings by embedding

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

Go
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.
  • Sort is not stable. Use sort.Stable when equal elements must keep their relative order.
  • The methods use value receivers on a slice type, which is fine because Swap changes the shared underlying array, not the slice header.
  • Embedding trick: keep Len and Swap on one base type and embed it in small structs that add only Less, 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: Interface can sort a linked structure or parallel slices (swap both slices in Swap) because it only talks in indexes.
  • Prefer sort.Slice or slices.SortFunc for one-off orderings; they need no extra type. Reach for Interface when 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.