"std:sort"Provides sorting, searching, and reversing for typed slices. Algorithm: Introsort (quicksort + heapsort fallback at depth 2*log2(n), insertion sort for n<=16). String comparison uses strings.Compare.
View source on Codeberg →type Interface struct { /* unexported fields */ }
Interface provides the generic sort contract. It holds a data value along with callback functions for determining length, comparing elements, and swapping elements. Use NewInterface to create one.
func NewInterface(data any, lenFn func(any) int, lessFn func(any, int, int) bool, swapFn func(any, int, int)) Interface
Creates an Interface for sorting arbitrary data. lenFn returns the number of elements, lessFn reports whether element i should sort before element j, and swapFn exchanges elements i and j.
func Sort(iface Interface)
Sorts the data described by the Interface using introsort (quicksort with heapsort fallback).
func IsSorted(iface Interface) bool
Reports whether the data described by the Interface is sorted in ascending order according to its less function.
func Reverse(iface Interface)
Reverses the order of elements described by the Interface.
func Search(n int, data any, f func(any, int) bool) int
Uses binary search to find the smallest index i in [0, n) at which f(data, i) is true, assuming f(data, i) == true implies f(data, i+1) == true. Returns n if no such index exists.
func Ints(arr []int)
Sorts a slice of ints in ascending order using Introsort.
func Float64s(arr []float64)
Sorts a slice of float64s in ascending order using Introsort.
func Strings(arr []string)
Sorts a slice of strings in lexicographic ascending order using Introsort. Comparison is performed via strings.Compare.
func IntsAreSorted(arr []int) bool
Reports whether a slice of ints is sorted in ascending order.
func Float64sAreSorted(arr []float64) bool
Reports whether a slice of float64s is sorted in ascending order.
func StringsAreSorted(arr []string) bool
Reports whether a slice of strings is sorted in lexicographic ascending order.
func SearchInts(arr []int, target int) int
Searches a sorted slice of ints for the target value using binary search. Returns the index of the target if found, or the index where it would be inserted to keep the array sorted.
func SearchStrings(arr []string, target string) int
Searches a sorted slice of strings for the target value using binary search. Returns the index of the target if found, or the index where it would be inserted to keep the array sorted.
func ReverseInts(arr []int)
Reverses a slice of ints in-place.
func ReverseFloat64s(arr []float64)
Reverses a slice of float64s in-place.
func ReverseStrings(arr []string)
Reverses a slice of strings in-place.