sort

sort.Ints, sort.Strings, sort.Slice, and sort.SliceStable, including stability and the Less comparator rules. Complements slices.Sort.

モジュールページの本文は現在英語です。

The sort package predates the generic slices package. sort.Ints, sort.Strings, and sort.Slice are still in the standard library. Official documentation for sort.Ints notes that as of Go 1.22 this function simply calls slices.Sort. For new code on Go 1.21 or later, slices.Sort and slices.SortStableFunc are usually the more direct choice. This page does not repeat the slices.Sort examples; it covers package sort, especially Slice versus SliceStable and the rules for a less function.

Examples were checked with Go 1.24.0.

Examples were checked against Go 1.24.0. See the official package documentation for the complete API.

Go reference overview →

API index

APISummary
sort.Ints, sort.Strings, sort.IntsAreSortedSort int or string slices in place and test whether an int slice is already sorted.
sort.SliceStable, sort.SliceSort a slice with a comparator. Use SliceStable when equal elements must keep their original order.

Ints, Strings, and IntsAreSorted

sort.Ints sorts a slice of ints in place, in increasing order. sort.Strings does the same for strings. sort.IntsAreSorted reports whether the slice is already in that order. These helpers do not return a new slice. The elements in the example are unique, so stability is not at issue.

sort.Ints, sort.Strings, sort.IntsAreSorted

func Ints(x []int)
func Strings(x []string)
func IntsAreSorted(x []int) bool

Sort int or string slices in place and test whether an int slice is already sorted.

ParameterDescription
xThe slice to sort or to inspect. Ints and Strings reorder x in place.

Returns: Ints and Strings do not return a value. IntsAreSorted returns whether x is in increasing order.

  • As of Go 1.22, official documentation says sort.Ints simply calls slices.Sort. See the slices page for the generic API.
go
package main

import (
	"fmt"
	"sort"
)

func main() {
	nums := []int{3, 1, 2}
	sort.Ints(nums)
	fmt.Println(nums, sort.IntsAreSorted(nums))
	names := []string{"c", "a", "b"}
	sort.Strings(names)
	fmt.Println(names)
}

Expected output

[1 2 3] true
[a b c]

Related: slices.Sort

Official documentation →

SliceStable and Less rules

sort.Slice is not guaranteed to be stable: official documentation says equal elements may be reversed from their original order. sort.SliceStable keeps equal elements in their original order. This example sorts by Age and asserts the stable result: age 1 first, then the two age-2 rows in the input order b, then a. This page does not assert the tie order of sort.Slice, because that order is not guaranteed.

The less function must satisfy the same requirements as sort.Interface.Less. Official documentation says that if both Less(i, j) and Less(j, i) are false, the elements are considered equal, and that Less must describe a transitive ordering. A less function that breaks those rules can mis-sort or panic. Both Slice and SliceStable panic if the first argument is not a slice.

sort.SliceStable, sort.Slice

func SliceStable(x any, less func(i, j int) bool)
func Slice(x any, less func(i, j int) bool)

Sort a slice with a comparator. Use SliceStable when equal elements must keep their original order.

ParameterDescription
xThe slice to sort in place. The call panics if x is not a slice.
lessless(i, j) reports whether the element at i must sort before the element at j. It must be transitive. When both less(i, j) and less(j, i) are false, the elements are equal for this sort.

Returns: Neither function returns a value. The slice is sorted in place.

Errors

  • panic — Official documentation: Slice and SliceStable panic if x is not a slice. They may also panic if less does not describe a valid ordering.
  • Official documentation also notes that slices.SortFunc and slices.SortStableFunc are often more ergonomic in new code. Those helpers live on the slices page.
go
package main

import (
	"fmt"
	"sort"
)

type row struct {
	Name string
	Age  int
}

func main() {
	rows := []row{{"b", 2}, {"a", 2}, {"c", 1}}
	sort.SliceStable(rows, func(i, j int) bool {
		return rows[i].Age < rows[j].Age
	})
	fmt.Println(rows)
}

Expected output

[{c 1} {b 2} {a 2}]

Related: slices.Sort

Official documentation →