Standard library / sort

sort.SliceStable()

Stably sorts a slice with the supplied less callback, preserving order of equivalent items. Panics if x is not a slice. Expected stdout: '[a bb cc]\n'.

Formal Syntax & Structure

go syntaxgo
func SliceStable(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{"bb", "a", "cc"}  sort.SliceStable(a, func(i, j int) bool { return len(a[i]) < len(a[j]) })  fmt.Println(a)}
Go Reference