HowtoGo
Home / Standard Library / The sort Package
Standard Library

The sort Package

sort orders slices and searches within sorted ones, either through built-in helpers, a custom Slice comparator, or the three-method Interface.

sort.Ints sorts a []int in place, ascending. The built-in-type functions cover this common case directly, no comparator required.

nums := []int{5, 2, 8, 1, 9}
sort.Ints(nums)
fmt.Println(nums)

Examples

Slice sorts anything shaped like a slice without a custom type. SliceStable costs a little more but keeps equal elements in their original relative order.

type Person struct {
    Name string
    Age  int
}

people := []Person{
    {"Bea", 30},
    {"Alan", 25},
    {"Cy", 25},
}

sort.SliceStable(people, func(i, j int) bool {
    return people[i].Age < people[j].Age
})

for _, p := range people {
    fmt.Println(p.Name, p.Age)
}
Output
Alan 25
Cy 25
Bea 30

Sorting slices

FunctionDescription
Slice(x any, less func(i, j int) bool)
sort.Slice(people, less)
Sorts x in place using less; x must be a slice, not guaranteed stable.
SliceStable(x any, less func(i, j int) bool)
sort.SliceStable(people, less)
Like Slice, but preserves the original order of elements less considers equal.
SliceIsSorted(x any, less func(i, j int) bool) bool
sort.SliceIsSorted(people, less)
Reports whether x is already sorted according to less.

sort.Interface

FunctionDescription
type Interface interface{ Len() int; Less(i, j int) bool; Swap(i, j int) }
type byLength []string
Three-method interface any type can implement to become sortable.
Sort(data Interface)
sort.Sort(words)
Sorts data in place; not guaranteed stable.
Stable(data Interface)
sort.Stable(words)
Like Sort, but preserves the original order of elements Less considers equal.
Reverse(data Interface) Interface
sort.Sort(sort.Reverse(words))
Wraps data so ascending Less reports descending order instead.
IsSorted(data Interface) bool
sort.IsSorted(words)
Reports whether data is already sorted.

Built-in type helpers

FunctionDescription
Ints(x []int)
sort.Ints(nums)
Sorts a []int in place, ascending.
IntsAreSorted(x []int) bool
sort.IntsAreSorted(nums)
Reports whether a []int is already sorted, ascending.
type IntSlice []int
sort.Sort(sort.Reverse(sort.IntSlice(nums)))
A []int with Len, Less, and Swap defined, so it satisfies Interface directly.
(IntSlice) Len() int / Less(i, j int) bool / Swap(i, j int)
Implements Interface for IntSlice; Less compares ascending.
(IntSlice) Sort()
Sorts the slice in place; equivalent to sort.Sort(x).
(IntSlice) Search(x int) int
Binary searches the slice for x; equivalent to sort.SearchInts(x, x).
Float64s(x []float64)
sort.Float64s(nums)
Sorts a []float64 in place, ascending. NaN values sort before all other values.
Float64sAreSorted(x []float64) bool
sort.Float64sAreSorted(nums)
Reports whether a []float64 is already sorted, ascending.
type Float64Slice []float64
sort.Sort(sort.Reverse(sort.Float64Slice(nums)))
A []float64 with Len, Less, and Swap defined, so it satisfies Interface directly.
(Float64Slice) Len() int / Less(i, j int) bool / Swap(i, j int)
Implements Interface for Float64Slice; Less compares ascending, treating NaN as less than any other value.
(Float64Slice) Sort()
Sorts the slice in place; equivalent to sort.Sort(x).
(Float64Slice) Search(x float64) int
Binary searches the slice for x; equivalent to sort.SearchFloat64s(x, x).
Strings(x []string)
sort.Strings(names)
Sorts a []string in place, ascending.
StringsAreSorted(x []string) bool
sort.StringsAreSorted(names)
Reports whether a []string is already sorted, ascending.
type StringSlice []string
sort.Sort(sort.Reverse(sort.StringSlice(names)))
A []string with Len, Less, and Swap defined, so it satisfies Interface directly.
(StringSlice) Len() int / Less(i, j int) bool / Swap(i, j int)
Implements Interface for StringSlice; Less compares ascending, byte-wise.
(StringSlice) Sort()
Sorts the slice in place; equivalent to sort.Sort(x).
(StringSlice) Search(x string) int
Binary searches the slice for x; equivalent to sort.SearchStrings(x, x).

Searching

FunctionDescription
Search(n int, f func(int) bool) int
sort.Search(len(a), f)
Binary searches [0,n) for the smallest index where f is true, assuming f is false then true across the range.
SearchInts(a []int, x int) int
sort.SearchInts(a, 42)
Binary searches a sorted []int for x, returning x's index or its insertion point.
SearchFloat64s(a []float64, x float64) int
sort.SearchFloat64s(a, 3.14)
Binary searches a sorted []float64 for x, returning x's index or its insertion point.
SearchStrings(a []string, x string) int
sort.SearchStrings(a, "go")
Binary searches a sorted []string for x, returning x's index or its insertion point.