Skip to content

Use slices and cmp packages instead of sort#4951

Open
pietern wants to merge 1 commit intomainfrom
go1.21-slices-package
Open

Use slices and cmp packages instead of sort#4951
pietern wants to merge 1 commit intomainfrom
go1.21-slices-package

Conversation

@pietern
Copy link
Copy Markdown
Contributor

@pietern pietern commented Apr 13, 2026

Summary

Migrate from the sort package to the slices and cmp packages across the codebase.

The slices package (stable since Go 1.21, extended in Go 1.23) provides type-safe, generic alternatives to sort. Go 1.23 added slices.Sorted and slices.SortedFunc, making the full migration more compelling since the sort import can often be dropped entirely.

Key replacements:

  • sort.Stringsslices.Sort
  • sort.Sliceslices.SortFunc
  • sort.SliceStableslices.SortStableFunc

The original prompt identified ~25 sort.Strings call sites and ~3 sort.SliceStable call sites across ~46 files importing sort. This is a low-risk, mechanical migration.

The .golangci.yaml configuration is updated to flag any new usage of the sort package.

Test plan

  • Existing unit tests pass (make test)
  • Linter passes (make lint)

This pull request was AI-assisted by Isaac.

Migrate all `sort.Slice`, `sort.SliceStable`, and `sort.Strings` calls
to their standard library equivalents introduced in Go 1.21:
`slices.SortFunc`, `slices.SortStableFunc`, and `slices.Sort`.

Add `forbidigo` lint rules to prevent reintroduction.

See https://go.dev/doc/go1.21#slices

Co-authored-by: Isaac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants