Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Collections: sort, slices, maps

Go sort.Slice() function

Reference for Go sort.Slice, SliceStable and SliceIsSorted: sort any slice with an index-based less function, multi-key sorting, stability and common mistakes.

sort.Slice(x, less) sorts a slice of any element type in place. You supply less(i, j), a function that reports whether the element at index i must come before the element at index j. That makes it the quickest way to sort a slice of structs by a field.

The sort is not stable: elements that compare equal may end up in a different order than they started. sort.SliceStable has the same signature and keeps equal elements in their original order.

Go
sort.Slice(users, func(i, j int) bool { return users[i].Age < users[j].Age })

Try it

Go Sort structs by a field

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 Multi-key and stable sorting

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 Slice(x any, less func(i, j int) bool)
func SliceStable(x any, less func(i, j int) bool)
func SliceIsSorted(x any, less func(i, j int) bool) bool
Name Description
x The slice to sort. It is passed as any and handled with reflection, so any slice type works.
less Reports whether element i must sort before element j. It must describe a strict weak ordering: less(i, i) is false, and if less(i, j) is true then less(j, i) is false.

Returns: Slice and SliceStable return nothing; SliceIsSorted returns whether the slice is already sorted according to less.

Panics: if x is not a slice.

Usage notes

  • Index into the same slice. The arguments are positions, so less must read books[i] and books[j] from the slice being sorted. Reading from a copy gives garbage because the elements are moved while sorting.
  • Use < (or >), never <=. A less function that returns true for equal elements breaks the ordering contract and can give wrong results.
  • Multi-key sorting: compare the primary key, return its result if the keys differ, and only then look at the next key. This is usually clearer than chaining several sorts.
  • Stability: use SliceStable when ties matter, or when you sort in several passes (sort by the least important key first).
  • Cost: O(n log n) comparisons, plus a reflection-based swapper. For large slices slices.SortFunc is faster and type-safe, and it takes a three-way comparison function returning an int.
  • Sorting a sub-range: pass a sub-slice (sort.Slice(x[2:8], ...)), but remember that less receives indexes relative to that sub-slice.
  • Nothing is returned, so keep a copy if you need the original order.