Sorting Slices
sort.Slice and helpers.
Sorting Slices is a free Go Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Sorting Matters
Sorting puts elements in a defined order. Go's sort package from the standard library handles this efficiently.
You rarely write your own sort algorithm in Go. Instead you call helpers that work on slices.
Importing sort
To use the package, import it with import "sort". It lives in the standard library, so no external download is needed.
package main
import (
"fmt"
"sort"
)
func main() {
fmt.Println("sort imported")
_ = sort.IntsAreSorted([]int{1, 2, 3})
}Sorting Integers
sort.Ints sorts a slice of int in ascending order, in place. The original slice is modified.
package main
import (
"fmt"
"sort"
)
func main() {
nums := []int{5, 2, 8, 1, 9}
sort.Ints(nums)
fmt.Println(nums)
}Sorting Strings
sort.Strings sorts a slice of strings lexicographically (alphabetical, byte order).
package main
import (
"fmt"
"sort"
)
func main() {
words := []string{"banana", "apple", "cherry"}
sort.Strings(words)
fmt.Println(words)
}Sorting Floats
sort.Float64s sorts a slice of float64 values in ascending order.
package main
import (
"fmt"
"sort"
)
func main() {
values := []float64{3.2, 1.5, 2.8}
sort.Float64s(values)
fmt.Println(values)
}sort.Slice
sort.Slice sorts any slice using a less function you supply. The function takes two indices i and j and returns true if element i should come before j.
package main
import (
"fmt"
"sort"
)
func main() {
nums := []int{5, 2, 8, 1}
sort.Slice(nums, func(i, j int) bool {
return nums[i] < nums[j]
})
fmt.Println(nums)
}Descending Order
To sort descending, flip the comparison in the less function: return nums[i] > nums[j].
package main
import (
"fmt"
"sort"
)
func main() {
nums := []int{5, 2, 8, 1}
sort.Slice(nums, func(i, j int) bool {
return nums[i] > nums[j]
})
fmt.Println(nums)
}Sorting Structs
sort.Slice shines with structs. You decide which field drives the order.
package main
import (
"fmt"
"sort"
)
type Person struct {
Name string
Age int
}
func main() {
people := []Person{{"Bob", 30}, {"Alice", 25}}
sort.Slice(people, func(i, j int) bool {
return people[i].Age < people[j].Age
})
fmt.Println(people)
}Checking If Sorted
Helpers like sort.IntsAreSorted and sort.SliceIsSorted return true if a slice is already sorted, without changing it.
package main
import (
"fmt"
"sort"
)
func main() {
nums := []int{1, 2, 3}
fmt.Println(sort.IntsAreSorted(nums))
}In-Place Modification
All these functions sort in place. They do not return a new slice. After calling sort.Ints(s), the variable s itself is reordered.
package main
import (
"fmt"
"sort"
)
func main() {
s := []int{3, 1, 2}
sort.Ints(s)
fmt.Println("s is now:", s)
}Choosing the Right Helper
Use the typed helpers (sort.Ints, sort.Strings, sort.Float64s) for simple slices. Reach for sort.Slice when you need a custom rule or sort structs.
sort.Ints- integers ascendingsort.Slice- anything, your rule
Quick Check
What does the less function in sort.Slice return?
Recap
You learned to sort with the sort package:
sort.Ints,sort.Strings,sort.Float64sfor typed slicessort.Slicewith a less function for custom rules and structs- All sorting happens in place
Frequently asked questions
Is the “Sorting Slices” lesson free?
Yes — the full text of “Sorting Slices” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Sorting Slices”?
sort.Slice and helpers. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Sorting Slices” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Sorting Slices
- Custom Sort Orders
- Searching Sorted Data
- Stable Sorting