Whether you search for golang sort, golang sort slice, go sort slice, sort slice golang, golang sort array, go sort array, sort in golang, or sort.slicestable, the standard library sort package is the usual answer. This guide covers convenience helpers (sort.Ints, sort.Strings, sort.Float64s), golang sort descending and golang reverse slice patterns, sort.Slice versus sort.SliceStable for stable ordering, sort.Sort with sort.IntSlice and sort.Reverse, and a short custom struct example. For interfaces in general, see that tutorial; this page stays focused on ordering data.
Tested with Go 1.24 on Linux.
Convenience: sort.Ints, sort.Strings, sort.Float64s
For plain []int, []string, and []float64 in ascending order, the helpers are the fastest path (what many people mean by go sort array of numbers or golang sort ints):
package main
import (
"fmt"
"sort"
)
func main() {
ints := []int{5, 2, 6, 3, 1, 4}
sort.Ints(ints)
fmt.Println("ints:", ints)
strs := []string{"daniel", "alice", "bob"}
sort.Strings(strs)
fmt.Println("strings:", strs)
floats := []float64{2.7, 1.4, 3.1}
sort.Float64s(floats)
fmt.Println("floats:", floats)
}You should see sorted ints, strings, and floats lines in ascending order.
Golang sort array: [N]T with a slice view
Go arrays have fixed length. sort.Ints expects a slice, so use a view such as arr[:] over the whole array (golang sort array / sort array golang):
package main
import (
"fmt"
"sort"
)
func main() {
var arr [5]int
arr[0], arr[1], arr[2], arr[3], arr[4] = 5, 6, 2, 7, 1
fmt.Println("before:", arr)
sort.Ints(arr[:])
fmt.Println("after:", arr)
}You should see before: [5 6 2 7 1] then after: [1 2 5 6 7].
sort.Slice (ascending)
sort.Slice sorts any slice given a less(i, j int) bool callback. The sort is not guaranteed to be stable; equal elements may swap. For stable behavior, see the next section (sort.SliceStable).
package main
import (
"fmt"
"sort"
)
func main() {
ints := []int{1, 13, 5, 2, 8, 11}
sort.Slice(ints, func(i, j int) bool { return ints[i] < ints[j] })
fmt.Println("ints:", ints)
strings := []string{"anna", "grey", "mono", "bob", "daniel", "alice"}
sort.Slice(strings, func(i, j int) bool { return strings[i] < strings[j] })
fmt.Println("strings:", strings)
floats := []float64{5.4, 2.4, 7.5, 1.5, 6.4}
sort.Slice(floats, func(i, j int) bool { return floats[i] < floats[j] })
fmt.Println("floats:", floats)
}You should see ascending ints, strings, and floats similar to the convenience example.
sort.SliceStable (stable sort.slice golang)
sort.SliceStable keeps the relative order of elements that compare equal—important when you sort by one field and ties should keep an earlier ordering. That matches searches for sort.slicestable behavior versus plain sort.Slice.
package main
import (
"fmt"
"sort"
)
func main() {
a := []int{2, 1, 2, 0}
sort.SliceStable(a, func(i, j int) bool { return a[i] < a[j] })
fmt.Println(a)
}You should see [0 1 2 2] with the first 2 still before the second 2 from the original slice.
Descending order (golang sort descending, reverse slice golang)
Two common patterns for golang sort slice descending:
- Flip the comparison in
sort.Slice(>instead of<). - Sort ascending with
sort.IntSlicethensort.Sort(sort.Reverse(...)).
package main
import (
"fmt"
"sort"
)
func main() {
byGreater := []int{1, 13, 5, 2, 8, 11}
sort.Slice(byGreater, func(i, j int) bool { return byGreater[i] > byGreater[j] })
fmt.Println("Slice desc:", byGreater)
withReverse := []int{1, 13, 5, 2, 8, 11}
sort.Sort(sort.Reverse(sort.IntSlice(withReverse)))
fmt.Println("Reverse:", withReverse)
}You should see two descending lines (largest value first).
sort.Sort with IntSlice, StringSlice, Float64Slice
Named slice types in the sort package implement sort.Interface. sort.Sort orders them ascending; pair with sort.Reverse for descending as shown above.
package main
import (
"fmt"
"sort"
)
func main() {
ints := []int{1, 13, 5, 2, 8, 11}
sort.Sort(sort.IntSlice(ints))
fmt.Println("ints:", ints)
strings := []string{"anna", "grey", "mono", "bob", "daniel", "alice"}
sort.Sort(sort.StringSlice(strings))
fmt.Println("strings:", strings)
floats := []float64{5.4, 2.4, 7.5, 1.5, 6.4}
sort.Sort(sort.Float64Slice(floats))
fmt.Println("floats:", floats)
}You should see the same ascending ordering as the sort.Ints / Strings / Float64s helpers.
Custom structs with sort.Slice
For slices of structs, define the comparison inline (no sort.Interface boilerplate unless you prefer it):
package main
import (
"fmt"
"sort"
)
type Person struct {
Name string
Age int
}
func main() {
people := []Person{{"Bob", 31}, {"Alice", 22}, {"Eve", 26}}
sort.Slice(people, func(i, j int) bool { return people[i].Age < people[j].Age })
fmt.Println(people)
}You should see ages ordered 22, 26, 31.
Stable vs unstable (short note)
sort.Ints, sort.Slice, and sort.Sort on the built-in named slices are not stable for equal elements. Use sort.SliceStable or sort.Stable when equal keys must keep their input order. For large inputs where stability does not matter, unstable sorts may allocate a bit less work internally—still O(n log n) for typical paths.
Summary
You can sort built-in slices with sort.Ints, sort.Strings, and sort.Float64s, sort arrays through a slice view such as arr[:], use sort.Slice and sort.SliceStable when you define the ordering with a less function, and use sort.Reverse with sort.Sort or a flipped comparison for descending order. Everything here mutates the backing slice or array view in place.

