w3soft.org by unix-world
0.00
How to Sort a Slice of ints in Golang
This example shows how to sort a slice of ints.
programming language: go lang 1.8 or later
operating system: any
Updated: 2026-02-25
Sort a slice of ints using the `sort` package
package main
import (
"fmt"
"sort"
)
func main() {
theSlice := []int{77, 88, 87, 78, 0, 7, 8}
fmt.Println("Original:", theSlice)
sort.Ints(theSlice) // Sorting in ascending order
fmt.Println("Sorted:", theSlice)
}
The outputof the program will be
Original: [77 88 87 78 0 7 8]
Sorted: [0 7 8 77 78 87 88]