Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Collections: sort, slices, maps

Go slices.Sort() and slices.SortFunc() functions

Reference for Go slices.Sort, SortFunc, SortStableFunc, IsSorted and BinarySearch: sort any slice generically, three-way comparison functions, cmp.Compare and cmp.Or.

slices.Sort(x) sorts a slice of any ordered type (integers, floats, strings) in increasing order, in place. For everything else, slices.SortFunc(x, cmp) sorts with a comparison function you write. Both come from the slices package (Go 1.21+) and replace most uses of sort.Ints, sort.Strings and sort.Slice.

The comparison function takes two elements, not two indexes, and returns an int: negative when a sorts before b, zero when they are equal, positive when a sorts after b. The package cmp provides cmp.Compare for exactly this.

Go
slices.SortFunc(people, func(a, b Person) int { return cmp.Compare(a.Age, b.Age) })

Try it

Go Sort and SortFunc

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 Stable sorting and checking order

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
func Sort[S ~[]E, E cmp.Ordered](x S)
func SortFunc[S ~[]E, E any](x S, cmp func(a, b E) int)
func SortStableFunc[S ~[]E, E any](x S, cmp func(a, b E) int)
func IsSorted[S ~[]E, E cmp.Ordered](x S) bool
func IsSortedFunc[S ~[]E, E any](x S, cmp func(a, b E) int) bool
func BinarySearch[S ~[]E, E cmp.Ordered](x S, target E) (int, bool)
func BinarySearchFunc[S ~[]E, E, T any](x S, target T, cmp func(E, T) int) (int, bool)
Name Description
x The slice, sorted in place.
cmp Three-way comparison returning a negative, zero or positive int. It must be consistent: cmp(a, b) == -cmp(b, a).
target The value to find in BinarySearch; in BinarySearchFunc it can have a different type than the elements (such as a key).

Returns: the sorting functions return nothing. BinarySearch returns the position of the first element not less than the target and whether it is equal to it.

Edge cases: for floats, NaN sorts before every other value. The comparison function must not modify the slice.

Usage notes

  • Sort needs cmp.Ordered: all integer and float types, and strings (including named types based on them). Structs and booleans are not ordered; use SortFunc.
  • SortFunc is not stable. Use SortStableFunc when equal elements must keep their order. Stable sorting is a little slower.
  • Compared with sort.Slice: the function receives values, not indexes, so you cannot accidentally read the wrong slice, and there is no reflection. It is typically measurably faster.
  • Return an int, not a bool. Returning a < b as a bool does not compile. Use cmp.Compare(a, b) for ordered types and strings.Compare for strings.
  • Multi-key sorts: cmp.Or returns the first non-zero comparison result, so cmp.Or(cmp.Compare(a.X, b.X), cmp.Compare(a.Y, b.Y)) sorts by X, then Y.
  • Descending order: swap the arguments in the comparison. There is no Reverse wrapper; slices.Reverse after sorting also works but costs another pass.
  • Large element types: sorting moves whole elements. For big structs, sort a slice of pointers or indexes instead.
  • Sorted keys of a map: slices.Sorted(maps.Keys(m)) collects and sorts in one call.
  • Complexity: O(n log n), using pattern-defeating quicksort; SortStableFunc uses insertion sort and symmerge.