Standard library / slices
slices.SortFunc()
Sorts in place using a strict weak ordering comparator; equal elements need not retain their original order. Expected stdout: '[3 2 1]\n'.
Formal Syntax & Structure
go syntaxgo
func SortFunc[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 := []int{1, 3, 2} slices.SortFunc(a, func(a, b int) int { return cmp.Compare(b, a) }) fmt.Println(a)}