[Go] Sort a slice

Created on November 30, 2026

Pass a slice and a comparison function to sort.Slice. This example sorts arr in ascending order. sort.Slice may change the relative order of elements that compare equal.

package main

import (
    "fmt"
    "sort"
)

func main() {
    arr := []int{10, 3, 15, 1, 22, 11}
    sort.Slice(arr, func(i, j int) bool {
        return arr[i] < arr[j]
    })
    fmt.Println(arr)
}
[1 3 10 11 15 22]
Go