Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Collections: sort, slices, maps

Go sort package

Reference for the Go sort package: sort.Ints, Strings, Slice, SliceStable, Sort, Stable, Search and friends, stability, complexity and when to use slices instead.

The package sort sorts slices and user-defined collections, checks whether data is sorted, and does binary search on sorted data. It has three layers: ready-made functions for slices of int, string and float64; sort.Slice, which sorts any slice with a comparison function; and sort.Sort, which works with any type that implements sort.Interface.

Since Go 1.21 the generic package slices offers faster, type-safe versions of most of this (slices.Sort, slices.SortFunc, slices.BinarySearch). New code usually prefers slices, but sort is everywhere in existing code and remains fully supported.

Go
nums := []int{3, 1, 2}
sort.Ints(nums) // [1 2 3]

Try it

Go The main sort functions

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 Binary search

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

Function or type Description
sort.Ints(x []int), Strings, Float64s Sort a slice in increasing order, in place.
sort.IntsAreSorted, StringsAreSorted, Float64sAreSorted Report whether the slice is sorted.
sort.Slice(x any, less func(i, j int) bool) Sort any slice with an index-based less function. Not stable.
sort.SliceStable(x any, less) Same, but keeps the original order of equal elements.
sort.SliceIsSorted(x any, less) bool Check sortedness with a less function.
sort.Sort(data Interface), sort.Stable(data) Sort anything that implements Len, Less and Swap.
sort.IsSorted(data Interface) bool Check sortedness of an Interface.
sort.Reverse(data Interface) Interface Wrap a collection so it sorts in decreasing order.
sort.IntSlice, StringSlice, Float64Slice Slice types that already implement Interface; each also has a Sort method.
sort.Search(n int, f func(int) bool) int Smallest index in [0, n) where f is true (n if none).
sort.Find(n int, cmp func(int) int) (i int, found bool) Binary search with a three-way comparison.
sort.SearchInts, SearchStrings, SearchFloat64s Binary search in a sorted slice, returns the insertion index.

Edge cases: sort.Slice panics if its first argument is not a slice. The less function must define a strict weak ordering; an inconsistent one produces unspecified order (no crash). NaN in Float64s sorts before other values.

Usage notes

  • Sorting is in place. The slice you pass is modified; copy it first (slices.Clone) if you need the original.
  • Not stable by default. Sort and Slice may reorder equal elements. Use Stable or SliceStable when ties must keep their input order, for example when sorting by one column after another.
  • Complexity: O(n log n) comparisons. The implementation is pattern-defeating quicksort, and stable sorts use insertion sort blocks plus symmerge with O(n logยฒ n) swaps.
  • Binary search needs sorted input. Search on unsorted data returns a meaningless result. Always compare the element at the returned index to confirm it is the value you wanted.
  • The less function gets indexes, not values, in sort.Slice and Search. Index into the slice you are sorting, not into a copy.
  • Sort maps by key by collecting keys into a slice, sorting that, and then looking up values in order, because map iteration order is random.
  • Prefer slices for new code: slices.Sort(x) and slices.SortFunc(x, cmp) avoid reflection and interface calls and are noticeably faster.