Standard library / sort

sort.Slice()

Sorts a slice in place with an index-based less callback. Panics if x is not a slice; stability not guaranteed. Prefer slices.SortFunc for typed comparisons. Expected stdout: '[b aa ccc]\n'.

Formal Syntax & Structure

go syntaxgo
func Slice(x any, less func(i, j int) bool)

Parameters & Attributes

NameTypeDescription
xanyInput value or collection to process.
lessfunc(i, j int) boolOrdering callback comparing elements at the given indices.

Working Example

Example Snippetgo
package main
import (  "fmt"  "sort")
func main() {  a := []string{"aa", "b", "ccc"}  sort.Slice(a, func(i, j int) bool { return len(a[i]) < len(a[j]) })  fmt.Println(a)}
Go Reference