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