Standard library / slices
slices.Compare()
Lexicographically compares ordered slices; if the common prefix is equal, the shorter slice sorts first. NaN sorts before non-NaN. Expected stdout: '-1\n'.
Formal Syntax & Structure
go syntaxgo
func Compare[S ~[]E, E cmp.Ordered](s1, s2 S) intParameters & Attributes
| Name | Type | Description |
|---|---|---|
| s1 | S | First slice to compare. |
| s2 | S | Second slice to compare. |
Return Value
int
Lexicographically compares ordered slices; if the common prefix is equal, the shorter slice sorts first. NaN sorts before non-NaN.
Working Example
Example Snippetgo
package main
import ( "fmt" "slices")
func main() { fmt.Println(slices.Compare([]int{1, 2}, []int{1, 3}))}