sort.Ints, sort.Strings, sort.IntsAreSorted
func Ints(x []int)
func Strings(x []string)
func IntsAreSorted(x []int) boolSort int or string slices in place and test whether an int slice is already sorted.
| Parameter | Description |
|---|---|
x | The 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.
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