Golang sort slice and array: Ints, Slice, SliceStable, descending

Golang sort slice and go sort slice: sort.Ints, sort.Strings, golang sort array with a slice view, sort.Slice and sort.SliceStable, golang sort descending and reverse slice golang, sort.Sort with IntSlice and sort.Reverse—one guide for sort in golang and go sort array.

Published

Updated

Read time 4 min read

Reviewed byDeepak Prasad

Golang sort slice and array: Ints, Slice, SliceStable, descending

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):

go
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)
}
Output

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):

go
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)
}
Output

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).

go
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)
}
Output

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.

go
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)
}
Output

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:

  1. Flip the comparison in sort.Slice (> instead of <).
  2. Sort ascending with sort.IntSlice then sort.Sort(sort.Reverse(...)).
go
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)
}
Output

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.

go
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)
}
Output

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):

go
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)
}
Output

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.


References


Frequently Asked Questions

1. How do I golang sort slice of ints or strings?

Use sort.Ints or sort.Strings for ascending built-in order in place, or sort.Slice with a less function when you need custom comparison or types beyond the helpers.

2. How do I golang sort array (fixed [N]T) instead of a slice?

Pass a slice view of the array such as arr[:] to sort.Ints or sort.Slice; the array backing store is sorted because the slice shares the same memory.

3. What is the difference between sort.Slice and sort.SliceStable?

sort.Slice is not stable: equal elements may move relative to each other; sort.SliceStable keeps the original relative order of equal elements while sorting.

4. How do I golang sort descending or reverse slice golang?

Either flip the comparison in sort.Slice (greater-than instead of less-than) or sort ascending then apply sort.Sort(sort.Reverse(sort.IntSlice(x))) for named slice types.

5. When should I use sort.Interface instead of sort.Slice?

sort.Slice is usually enough; implement sort.Interface on a named type when you want reusable Len, Less, and Swap methods or to use sort.Sort and sort.Stable directly on your type.

6. Is sorting in Go in-place?

Yes; sort.Ints, sort.Strings, sort.Float64s, sort.Slice, and sort.Sort reorder the underlying slice or array view without allocating a new collection.
Tuan Nguyen

Data Scientist

Proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise …