Golang slices.IsSortedFunc
last modified April 20, 2025
This tutorial explains how to use the slices.IsSortedFunc
function in Go.
We'll cover slice operations with practical examples of checking sort order.
The slices.IsSortedFunc function tests whether a slice is sorted according to a custom comparison function. It's part of Go's experimental slices package.
This function is useful for verifying custom sort orders beyond simple ascending or descending sequences. It returns true if the slice is sorted as defined.
Basic slices.IsSortedFunc Example
The simplest use of slices.IsSortedFunc
checks if numbers are sorted
in ascending order. We define a comparison function that returns -1, 0, or 1.
package main import ( "cmp" "fmt" "slices" ) func main() { numbers := []int{1, 2, 3, 4, 5} isSorted := slices.IsSortedFunc(numbers, func(a, b int) int { return cmp.Compare(a, b) }) fmt.Println("Is sorted:", isSorted) }
We create a sorted slice and check its order using the standard comparison. The function returns true since the numbers are in ascending sequence.
Checking Descending Order
slices.IsSortedFunc
can verify descending order by reversing the
comparison logic. This example checks if numbers are sorted high to low.
package main import ( "cmp" "fmt" "slices" ) func main() { numbers := []int{5, 4, 3, 2, 1} isSorted := slices.IsSortedFunc(numbers, func(a, b int) int { return cmp.Compare(b, a) // Reverse comparison }) fmt.Println("Is sorted descending:", isSorted) }
By comparing b to a instead of a to b, we invert the sort order check. The function confirms our slice is properly sorted in descending order.
Custom Struct Sorting
We can use slices.IsSortedFunc
with custom struct types. This example
checks if people are sorted by age.
package main import ( "cmp" "fmt" "slices" ) type Person struct { Name string Age int } func main() { people := []Person{ {"Alice", 25}, {"Bob", 30}, {"Charlie", 35}, } isSorted := slices.IsSortedFunc(people, func(a, b Person) int { return cmp.Compare(a.Age, b.Age) }) fmt.Println("Sorted by age:", isSorted) }
The comparison function examines the Age field of each Person struct. Since ages increase sequentially, the function returns true.
Case-Insensitive String Sorting
slices.IsSortedFunc
can handle case-insensitive string comparisons.
This example checks if words are sorted alphabetically ignoring case.
package main import ( "fmt" "slices" "strings" ) func main() { words := []string{"apple", "Banana", "cherry"} isSorted := slices.IsSortedFunc(words, func(a, b string) int { return strings.Compare(strings.ToLower(a), strings.ToLower(b)) }) fmt.Println("Case-insensitive sorted:", isSorted) }
We convert strings to lowercase before comparing to ignore case differences. The function confirms our slice follows alphabetical order regardless of case.
Empty and Single-Element Slices
slices.IsSortedFunc
has special behavior for empty and single-element
slices. These cases are always considered sorted.
package main import ( "fmt" "slices" ) func main() { empty := []int{} single := []string{"hello"} emptySorted := slices.IsSortedFunc(empty, func(a, b int) int { return 0 // Doesn't matter }) singleSorted := slices.IsSortedFunc(single, func(a, b string) int { return 0 // Doesn't matter }) fmt.Println("Empty sorted:", emptySorted) fmt.Println("Single-element sorted:", singleSorted) }
With zero or one elements, there are no pairs to compare. The function returns true by definition for these cases.
Complex Custom Sorting
We can implement complex sorting logic with slices.IsSortedFunc
.
This example checks if points are sorted by distance from origin.
package main import ( "fmt" "math" "slices" ) type Point struct { X, Y float64 } func main() { points := []Point{ {1, 1}, {2, 2}, {3, 3}, } isSorted := slices.IsSortedFunc(points, func(a, b Point) int { distA := math.Sqrt(a.X*a.X + a.Y*a.Y) distB := math.Sqrt(b.X*b.X + b.Y*b.Y) if distA < distB { return -1 } else if distA > distB { return 1 } return 0 }) fmt.Println("Sorted by distance:", isSorted) }
We calculate each point's distance from the origin before comparing. The function verifies the points are ordered by increasing distance.
Practical Example: Version Numbers
This practical example checks if version strings are sorted correctly, handling the dotted-number format appropriately.
package main import ( "fmt" "slices" "strconv" "strings" ) func main() { versions := []string{"1.2", "1.10", "1.9", "2.0"} isSorted := slices.IsSortedFunc(versions, func(a, b string) int { aParts := strings.Split(a, ".") bParts := strings.Split(b, ".") for i := 0; i < len(aParts) && i < len(bParts); i++ { aNum, _ := strconv.Atoi(aParts[i]) bNum, _ := strconv.Atoi(bParts[i]) if aNum != bNum { return aNum - bNum } } return len(aParts) - len(bParts) }) fmt.Println("Versions sorted correctly:", isSorted) }
We split version strings into components and compare numerically. The function returns false because "1.10" should come after "1.9".
Source
Go experimental slices package documentation
This tutorial covered the slices.IsSortedFunc
function in Go with practical
examples of checking custom sort orders in various scenarios.
Author
List all Go tutorials.