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) []E

Parameters & Attributes

NameTypeDescription
seqiter.Seq[E]Iterator function producing the input sequence.
cmpfunc(E, E) intComparison 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)) }))}
Go Reference