Skip to main content
Package sort provides primitives for sorting slices and user-defined collections.
In many situations, the newer slices.SortFunc function is more ergonomic and runs faster.

Types

Interface

type Interface interface {
    Len() int
    Less(i, j int) bool
    Swap(i, j int)
}
An implementation of Interface can be sorted by the routines in this package. The methods refer to elements of the underlying collection by integer index.
Len
func() int
Returns the number of elements in the collection
Less
func(i, j int) bool
Reports whether the element with index i must sort before the element with index j. Must describe a strict weak ordering.
Swap
func(i, j int)
Swaps the elements with indexes i and j

Convenience Types

IntSlice

type IntSlice []int
Attaches the methods of Interface to []int, sorting in increasing order. Methods:
  • func (x IntSlice) Len() int
  • func (x IntSlice) Less(i, j int) bool
  • func (x IntSlice) Swap(i, j int)
  • func (x IntSlice) Sort()
  • func (x IntSlice) Search(x int) int

Float64Slice

type Float64Slice []float64
Implements Interface for []float64, sorting in increasing order with NaN values ordered before other values. Methods:
  • func (x Float64Slice) Len() int
  • func (x Float64Slice) Less(i, j int) bool
  • func (x Float64Slice) Swap(i, j int)
  • func (x Float64Slice) Sort()
  • func (x Float64Slice) Search(x float64) int

StringSlice

type StringSlice []string
Attaches the methods of Interface to []string, sorting in increasing order. Methods:
  • func (x StringSlice) Len() int
  • func (x StringSlice) Less(i, j int) bool
  • func (x StringSlice) Swap(i, j int)
  • func (x StringSlice) Sort()
  • func (x StringSlice) Search(x string) int

Sorting Functions

Sort

func Sort(data Interface)
Sorts data in ascending order as determined by the Less method. It makes one call to data.Len to determine n and O(n*log(n)) calls to data.Less and data.Swap. The sort is not guaranteed to be stable.
data
Interface
required
The collection to sort
complexity
string
O(n*log(n)) comparisons and swaps

Stable

func Stable(data Interface)
Sorts data in ascending order as determined by the Less method, while keeping the original order of equal elements.
data
Interface
required
The collection to sort
complexity
string
O(nlog(n)) comparisons, O(nlog(n)*log(n)) swaps

IsSorted

func IsSorted(data Interface) bool
Reports whether data is sorted.
data
Interface
required
The collection to check
return
bool
true if data is sorted, false otherwise

Reverse

func Reverse(data Interface) Interface
Returns the reverse order for data.
data
Interface
required
The collection to reverse
return
Interface
An Interface wrapper that reverses the ordering

Slice Functions

Slice

func Slice(x any, less func(i, j int) bool)
Sorts the slice x given the provided less function. It panics if x is not a slice. The sort is not guaranteed to be stable.
x
any
required
The slice to sort (must be a slice type)
less
func(i, j int) bool
required
Comparison function that reports whether x[i] should sort before x[j]
The newer slices.SortFunc function is more ergonomic and runs faster.

SliceStable

func SliceStable(x any, less func(i, j int) bool)
Sorts the slice x using the provided less function, keeping equal elements in their original order. It panics if x is not a slice.
x
any
required
The slice to sort (must be a slice type)
less
func(i, j int) bool
required
Comparison function that reports whether x[i] should sort before x[j]

SliceIsSorted

func SliceIsSorted(x any, less func(i, j int) bool) bool
Reports whether the slice x is sorted according to the provided less function. It panics if x is not a slice.
x
any
required
The slice to check (must be a slice type)
less
func(i, j int) bool
required
Comparison function
return
bool
true if x is sorted, false otherwise

Convenience Functions

Ints

func Ints(x []int)
Sorts a slice of ints in increasing order.
x
[]int
required
The slice to sort
As of Go 1.22, this function simply calls slices.Sort.

Float64s

func Float64s(x []float64)
Sorts a slice of float64s in increasing order. NaN values are ordered before other values.
x
[]float64
required
The slice to sort

Strings

func Strings(x []string)
Sorts a slice of strings in increasing order.
x
[]string
required
The slice to sort

IntsAreSorted

func IntsAreSorted(x []int) bool
Reports whether the slice x is sorted in increasing order.
x
[]int
required
The slice to check

Float64sAreSorted

func Float64sAreSorted(x []float64) bool
Reports whether the slice x is sorted in increasing order, with NaN values before any other values.
x
[]float64
required
The slice to check

StringsAreSorted

func StringsAreSorted(x []string) bool
Reports whether the slice x is sorted in increasing order.
x
[]string
required
The slice to check

Binary Search Functions

func Search(n int, f func(int) bool) int
Uses binary search to find and return the smallest index i in [0, n) at which f(i) is true. Search requires that f is false for some (possibly empty) prefix of the input range [0, n) and then true for the remainder. If there is no such index, Search returns n.
n
int
required
The upper bound of the search range
f
func(int) bool
required
Predicate function
return
int
The smallest index i where f(i) is true, or n if no such index exists

Find

func Find(n int, cmp func(int) int) (i int, found bool)
Uses binary search to find and return the smallest index i in the range from 0 to n at which the comparison function returns a value less than or equal to 0. The found result is true if i is less than n and the comparison returns exactly 0.
n
int
required
The upper bound of the search range
cmp
func(int) int
required
Comparison function returning negative, zero, or positive
i
int
The index where the target is found or should be inserted
found
bool
true if the target was found, false otherwise

SearchInts

func SearchInts(a []int, x int) int
Searches for x in a sorted slice of ints and returns the index as specified by Search. The slice must be sorted in ascending order.
a
[]int
required
The sorted slice to search
x
int
required
The value to search for
return
int
The index where x is found or should be inserted

SearchFloat64s

func SearchFloat64s(a []float64, x float64) int
Searches for x in a sorted slice of float64s and returns the index as specified by Search. The slice must be sorted in ascending order.
a
[]float64
required
The sorted slice to search
x
float64
required
The value to search for

SearchStrings

func SearchStrings(a []string, x string) int
Searches for x in a sorted slice of strings and returns the index as specified by Search. The slice must be sorted in ascending order.
a
[]string
required
The sorted slice to search
x
string
required
The value to search for

Examples

Basic Sorting

package main

import (
    "fmt"
    "sort"
)

func main() {
    // Sort integers
    ints := []int{5, 2, 6, 3, 1, 4}
    sort.Ints(ints)
    fmt.Println(ints) // [1 2 3 4 5 6]

    // Sort strings
    strs := []string{"c", "a", "b"}
    sort.Strings(strs)
    fmt.Println(strs) // [a b c]

    // Check if sorted
    fmt.Println(sort.IntsAreSorted(ints)) // true
}

Custom Sort with Interface

package main

import (
    "fmt"
    "sort"
)

type Person struct {
    Name string
    Age  int
}

type ByAge []Person

func (a ByAge) Len() int           { return len(a) }
func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
func (a ByAge) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func main() {
    people := []Person{
        {"Bob", 31},
        {"John", 42},
        {"Michael", 17},
        {"Jenny", 26},
    }

    sort.Sort(ByAge(people))
    fmt.Println(people)
    // [{Michael 17} {Jenny 26} {Bob 31} {John 42}]
}

Sort with Slice Function

package main

import (
    "fmt"
    "sort"
)

type Person struct {
    Name string
    Age  int
}

func main() {
    people := []Person{
        {"Bob", 31},
        {"John", 42},
        {"Michael", 17},
        {"Jenny", 26},
    }

    // Sort by age
    sort.Slice(people, func(i, j int) bool {
        return people[i].Age < people[j].Age
    })

    fmt.Println(people)
    // [{Michael 17} {Jenny 26} {Bob 31} {John 42}]
}
package main

import (
    "fmt"
    "sort"
)

func main() {
    data := []int{1, 2, 3, 5, 8, 13, 21}
    x := 8

    i := sort.Search(len(data), func(i int) bool { return data[i] >= x })
    if i < len(data) && data[i] == x {
        fmt.Printf("found %d at index %d\n", x, i)
    } else {
        fmt.Printf("%d not found, would insert at %d\n", x, i)
    }
    // Output: found 8 at index 4
}

Build docs developers (and LLMs) love