Standard library / slices
slices.SortedStableFunc()
Collects and stably sorts iterator values, preserving order among equivalent elements. Expected stdout: '[a bb cc]\n'.
Formal Syntax & Structure
go syntaxgo
func SortedStableFunc[E any](seq iter.Seq[E], cmp func(E, E) int) []EParameters & Attributes
| Name | Type | Description |
|---|---|---|
| seq | iter.Seq[E] | Iterator function producing the input sequence. |
| cmp | func(E, E) int | Comparison callback; negative, zero, or positive for less, equivalent, or greater. |
Return Value
[]E
Collects and stably sorts iterator values, preserving order among equivalent elements.
Working Example
Example Snippetgo
package main
import ( "cmp" "fmt" "slices")
func main() { fmt.Println(slices.SortedStableFunc(slices.Values([]string{"bb", "a", "cc"}), func(a, b string) int { return cmp.Compare(len(a), len(b)) }))}