sort orders slices and searches within sorted ones, either through built-in helpers, a custom Slice comparator, or the three-method Interface.
sort.Ints sorts a []int in place, ascending. The built-in-type functions cover this common case directly, no comparator required.
nums := []int{5, 2, 8, 1, 9}
sort.Ints(nums)
fmt.Println(nums)Examples
Slice sorts anything shaped like a slice without a custom type. SliceStable costs a little more but keeps equal elements in their original relative order.
type Person struct {
Name string
Age int
}
people := []Person{
{"Bea", 30},
{"Alan", 25},
{"Cy", 25},
}
sort.SliceStable(people, func(i, j int) bool {
return people[i].Age < people[j].Age
})
for _, p := range people {
fmt.Println(p.Name, p.Age)
}Alan 25
Cy 25
Bea 30Implementing Len, Less, and Swap makes any type sortable with Sort. Reverse wraps that same Interface to flip the ordering without writing a second Less.
type byLength []string
func (s byLength) Len() int { return len(s) }
func (s byLength) Less(i, j int) bool { return len(s[i]) < len(s[j]) }
func (s byLength) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
words := byLength{"banana", "kiwi", "fig"}
sort.Sort(words)
fmt.Println(words)
sort.Sort(sort.Reverse(words))
fmt.Println(words)[fig kiwi banana]
[banana kiwi fig]Ints, Float64s, and Strings skip the Interface entirely for the three built-in types they cover, sorting the slice directly.
nums := []int{5, 2, 8, 1}
sort.Ints(nums)
fmt.Println(nums)
fmt.Println(sort.IntsAreSorted(nums))
names := []string{"go", "banana", "ada"}
sort.Strings(names)
fmt.Println(names)[1 2 5 8]
true
[ada banana go]SearchInts and the lower-level Search both run a binary search, so they only give a meaningful result on an already-sorted slice. A miss returns the index where the value would be inserted.
nums := []int{4, 11, 19, 27, 34, 42, 50, 58, 66}
i := sort.SearchInts(nums, 42)
fmt.Println(i)
i = sort.SearchInts(nums, 45)
fmt.Println(i)
idx := sort.Search(len(nums), func(i int) bool {
return nums[i] >= 50
})
fmt.Println(idx)5
6
6Sorting slices
| Function | Description |
|---|---|
Slice(x any, less func(i, j int) bool) sort.Slice(people, less) | Sorts x in place using less; x must be a slice, not guaranteed stable. |
SliceStable(x any, less func(i, j int) bool) sort.SliceStable(people, less) | Like Slice, but preserves the original order of elements less considers equal. |
SliceIsSorted(x any, less func(i, j int) bool) bool sort.SliceIsSorted(people, less) | Reports whether x is already sorted according to less. |
sort.Interface
| Function | Description |
|---|---|
type Interface interface{ Len() int; Less(i, j int) bool; Swap(i, j int) } type byLength []string | Three-method interface any type can implement to become sortable. |
Sort(data Interface) sort.Sort(words) | Sorts data in place; not guaranteed stable. |
Stable(data Interface) sort.Stable(words) | Like Sort, but preserves the original order of elements Less considers equal. |
Reverse(data Interface) Interface sort.Sort(sort.Reverse(words)) | Wraps data so ascending Less reports descending order instead. |
IsSorted(data Interface) bool sort.IsSorted(words) | Reports whether data is already sorted. |
Built-in type helpers
| Function | Description |
|---|---|
Ints(x []int) sort.Ints(nums) | Sorts a []int in place, ascending. |
IntsAreSorted(x []int) bool sort.IntsAreSorted(nums) | Reports whether a []int is already sorted, ascending. |
type IntSlice []int sort.Sort(sort.Reverse(sort.IntSlice(nums))) | A []int with Len, Less, and Swap defined, so it satisfies Interface directly. |
(IntSlice) Len() int / Less(i, j int) bool / Swap(i, j int) | Implements Interface for IntSlice; Less compares ascending. |
(IntSlice) Sort() | Sorts the slice in place; equivalent to sort.Sort(x). |
(IntSlice) Search(x int) int | Binary searches the slice for x; equivalent to sort.SearchInts(x, x). |
Float64s(x []float64) sort.Float64s(nums) | Sorts a []float64 in place, ascending. NaN values sort before all other values. |
Float64sAreSorted(x []float64) bool sort.Float64sAreSorted(nums) | Reports whether a []float64 is already sorted, ascending. |
type Float64Slice []float64 sort.Sort(sort.Reverse(sort.Float64Slice(nums))) | A []float64 with Len, Less, and Swap defined, so it satisfies Interface directly. |
(Float64Slice) Len() int / Less(i, j int) bool / Swap(i, j int) | Implements Interface for Float64Slice; Less compares ascending, treating NaN as less than any other value. |
(Float64Slice) Sort() | Sorts the slice in place; equivalent to sort.Sort(x). |
(Float64Slice) Search(x float64) int | Binary searches the slice for x; equivalent to sort.SearchFloat64s(x, x). |
Strings(x []string) sort.Strings(names) | Sorts a []string in place, ascending. |
StringsAreSorted(x []string) bool sort.StringsAreSorted(names) | Reports whether a []string is already sorted, ascending. |
type StringSlice []string sort.Sort(sort.Reverse(sort.StringSlice(names))) | A []string with Len, Less, and Swap defined, so it satisfies Interface directly. |
(StringSlice) Len() int / Less(i, j int) bool / Swap(i, j int) | Implements Interface for StringSlice; Less compares ascending, byte-wise. |
(StringSlice) Sort() | Sorts the slice in place; equivalent to sort.Sort(x). |
(StringSlice) Search(x string) int | Binary searches the slice for x; equivalent to sort.SearchStrings(x, x). |
Searching
| Function | Description |
|---|---|
Search(n int, f func(int) bool) int sort.Search(len(a), f) | Binary searches [0,n) for the smallest index where f is true, assuming f is false then true across the range. |
SearchInts(a []int, x int) int sort.SearchInts(a, 42) | Binary searches a sorted []int for x, returning x's index or its insertion point. |
SearchFloat64s(a []float64, x float64) int sort.SearchFloat64s(a, 3.14) | Binary searches a sorted []float64 for x, returning x's index or its insertion point. |
SearchStrings(a []string, x string) int sort.SearchStrings(a, "go") | Binary searches a sorted []string for x, returning x's index or its insertion point. |