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
| Name | Type | Description |
|---|---|---|
| x | S | Input value or collection to process. |
| cmp | func(a, b E) int | Comparison 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)}