Standard library / slices

slices.SortStableFunc()

Sorts in place and preserves original order among comparator-equivalent elements. Expected stdout: '[a bb cc]\n'.

Formal Syntax & Structure

go syntaxgo
func SortStableFunc[S ~[]E, E any](x S, cmp func(a, b E) int)

Parameters & Attributes

NameTypeDescription
xSInput value or collection to process.
cmpfunc(a, b E) intComparison callback; negative, zero, or positive for less, equivalent, or greater.

Working Example

Example Snippetgo
package main
import (  "cmp"  "fmt"  "slices")
func main() {  a := []string{"bb", "a", "cc"}  slices.SortStableFunc(a, func(a, b string) int { return cmp.Compare(len(a), len(b)) })  fmt.Println(a)}
Go Reference