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
| Name | Type | Description |
|---|---|---|
| x | any | Input value or collection to process. |
| less | func(i, j int) bool | Ordering 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)}