sort

Data
import "sort"

Provides primitives for sorting slices and user-defined collections.

Example

package main

import (
    "fmt"
    "sort"
)

func main() {
    nums := []int{3, 1, 4, 1, 5, 9}
    sort.Ints(nums)
    fmt.Println(nums)  // [1 1 3 4 5 9]
}

Key Types & Functions

IntsStringsSliceSortSearchInterface

About sort

The sort package (imported as sort) belongs to the Data category of Go packages. Provides primitives for sorting slices and user-defined collections.

Go's standard library is one of the language's greatest strengths, providing production-ready implementations for networking, cryptography, encoding, I/O, and more. The sort package follows Go's philosophy of simplicity and composability — small, focused packages that combine through interfaces like io.Reader and io.Writer.

When using sort in production, follow Go best practices: handle errors explicitly, use context for cancellation and timeouts, prefer composition over inheritance, and write table-driven tests. The Go documentation at pkg.go.dev provides comprehensive API references and examples for every exported type and function.

Related Packages