Skip to content

Sorting

The sort package provides sorting for built‑in types and custom sorts via sort.Interface. For slices of ints, floats, or strings, use sort.Ints, sort.Float64s, sort.Strings.

Sort slices of ints and strings.

package main
import (
"fmt"
"sort"
)
func main() {
ints := []int{5, 2, 8, 1, 9}
sort.Ints(ints)
fmt.Println(ints)
strs := []string{"banana", "apple", "cherry"}
sort.Strings(strs)
fmt.Println(strs)
// Check if sorted
fmt.Println(sort.IntsAreSorted(ints))
}
Terminal window
[1 2 5 8 9]
[apple banana cherry]
true