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

NameTypeDescription
xSInput value or collection to process.
cmpfunc(a, b E) intComparison 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)}
Go Reference