All packages

    sort

    import "sort"

    Package sort provides primitives for sorting slices and user-defined collections.

    Go 1.26.1

    Functions

    func Find(n int, cmp func(...)) (int, bool)

    Find uses binary search to find and return the smallest index i in [0, n) at which cmp(i) <= 0. If there is no such index i, Find returns i = n. The found result is true if i < n and cmp(i) == 0. Find calls cmp(i) only for i in the range [0, n). To permit binary search, Find requires that cmp(i) > 0 for a leading prefix of the range, cmp(i) == 0 in the middle, and cmp(i) < 0 for the final suffix of the range. (Each subrange could be empty.) The usual way to establish this condition is to int...

    sort/search.go:99

    func Float64s(x []float64)

    Float64s sorts a slice of float64s in increasing order. Not-a-number (NaN) values are ordered before other values. Note: as of Go 1.22, this function simply calls [slices.Sort].

    sort/sort.go:176

    func Float64sAreSorted(x []float64) bool

    Float64sAreSorted reports whether the slice x is sorted in increasing order, with not-a-number (NaN) values before any other values. Note: as of Go 1.22, this function simply calls [slices.IsSorted].

    sort/sort.go:192

    func Ints(x []int)

    Ints sorts a slice of ints in increasing order. Note: as of Go 1.22, this function simply calls [slices.Sort].

    sort/sort.go:170

    func IntsAreSorted(x []int) bool

    IntsAreSorted reports whether the slice x is sorted in increasing order. Note: as of Go 1.22, this function simply calls [slices.IsSorted].

    sort/sort.go:186

    func IsSorted(data Interface) bool

    IsSorted reports whether data is sorted. Note: in many situations, the newer [slices.IsSortedFunc] function is more ergonomic and runs faster.

    sort/sort.go:110

    func Reverse(data Interface) Interface

    Reverse returns the reverse order for data.

    sort/sort.go:102

    func SearchFloat64s(a []float64, x float64) int

    SearchFloat64s searches for x in a sorted slice of float64s and returns the index as specified by [Search]. The return value is the index to insert x if x is not present (it could be len(a)). The slice must be sorted in ascending order.

    sort/search.go:131

    func SearchInts(a []int, x int) int

    SearchInts searches for x in a sorted slice of ints and returns the index as specified by [Search]. The return value is the index to insert x if x is not present (it could be len(a)). The slice must be sorted in ascending order.

    sort/search.go:123

    func SearchStrings(a []string, x string) int

    SearchStrings searches for x in a sorted slice of strings and returns the index as specified by Search. The return value is the index to insert x if x is not present (it could be len(a)). The slice must be sorted in ascending order.

    sort/search.go:139

    func Slice(x any, less func(...))

    Slice 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: equal elements may be reversed from their original order. For a stable sort, use [SliceStable]. The less function must satisfy the same requirements as the Interface type's Less method. Note: in many situations, the newer [slices.SortFunc] function is more ergonomic and runs faster.

    sort/slice.go:24

    func SliceIsSorted(x any, less func(...)) bool

    SliceIsSorted reports whether the slice x is sorted according to the provided less function. It panics if x is not a slice. Note: in many situations, the newer [slices.IsSortedFunc] function is more ergonomic and runs faster.

    sort/slice.go:52

    func SliceStable(x any, less func(...))

    SliceStable sorts the slice x using the provided less function, keeping equal elements in their original order. It panics if x is not a slice. The less function must satisfy the same requirements as the Interface type's Less method. Note: in many situations, the newer [slices.SortStableFunc] function is more ergonomic and runs faster.

    sort/slice.go:41

    func Sort(data Interface)

    Sort 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. Note: in many situations, the newer [slices.SortFunc] function is more ergonomic and runs faster.

    sort/sort.go:50

    func Stable(data Interface)

    Stable sorts data in ascending order as determined by the Less method, while keeping the original order of equal elements. It makes one call to data.Len to determine n, O(n*log(n)) calls to data.Less and O(n*log(n)*log(n)) calls to data.Swap. Note: in many situations, the newer slices.SortStableFunc function is more ergonomic and runs faster.

    sort/sort.go:233

    func Strings(x []string)

    Strings sorts a slice of strings in increasing order. Note: as of Go 1.22, this function simply calls [slices.Sort].

    sort/sort.go:181

    func StringsAreSorted(x []string) bool

    StringsAreSorted reports whether the slice x is sorted in increasing order. Note: as of Go 1.22, this function simply calls [slices.IsSorted].

    sort/sort.go:197

    Types

    type Float64Slice []float64

    Float64Slice implements Interface for a []float64, sorting in increasing order, with not-a-number (NaN) values ordered before other values.

    sort/sort.go:134

    func (x Float64Slice) Len() int

    sort/sort.go:136

    func (x Float64Slice) Less(i int, j int) bool

    Less reports whether x[i] should be ordered before x[j], as required by the sort Interface. Note that floating-point comparison by itself is not a transitive relation: it does not report a consistent ordering for not-a-number (NaN) values. This implementation of Less places NaN values before any others, by using: x[i] < x[j] || (math.IsNaN(x[i]) && !math.IsNaN(x[j]))

    sort/sort.go:144

    func (x Float64Slice) Sort()

    Sort is a convenience method: x.Sort() calls Sort(x).

    sort/sort.go:153

    func (x Float64Slice) Swap(i int, j int)

    sort/sort.go:145

    type Interface interface {
    	// Len is the number of elements in the collection.
    	Len() int
    
    	// Less reports whether the element with index i
    	// must sort before the element with index j.
    	//
    	// ...

    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.

    sort/sort.go:17

    type IntSlice []int

    IntSlice attaches the methods of Interface to []int, sorting in increasing order.

    sort/sort.go:123

    func (x IntSlice) Len() int

    sort/sort.go:125

    func (x IntSlice) Less(i int, j int) bool

    sort/sort.go:126

    func (x IntSlice) Sort()

    Sort is a convenience method: x.Sort() calls Sort(x).

    sort/sort.go:130

    func (x IntSlice) Swap(i int, j int)

    sort/sort.go:127

    type StringSlice []string

    StringSlice attaches the methods of Interface to []string, sorting in increasing order.

    sort/sort.go:156

    func (x StringSlice) Len() int

    sort/sort.go:158

    func (x StringSlice) Less(i int, j int) bool

    sort/sort.go:159

    func (x StringSlice) Sort()

    Sort is a convenience method: x.Sort() calls Sort(x).

    sort/sort.go:163

    func (x StringSlice) Swap(i int, j int)

    sort/sort.go:160