Standard library / slices

slices.SortedFunc()

Collects and sorts iterator values using a comparator; equal values need not retain original order. Expected stdout: '[3 2 1]\n'.

Formal Syntax & Structure

go syntaxgo
func SortedFunc[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 sorts iterator values using a comparator; equal values need not retain original order.

Working Example

Example Snippetgo
package main
import (  "cmp"  "fmt"  "slices")
func main() {  fmt.Println(slices.SortedFunc(slices.Values([]int{1, 3, 2}), func(a, b int) int { return cmp.Compare(b, a) }))}
Go Reference