Skip to content

Use slices.Sorted(maps.Keys(...)) to replace collect-keys-then-sort pattern#4954

Open
pietern wants to merge 2 commits intogo1.21-slices-packagefrom
go1.23-sorted-map-keys
Open

Use slices.Sorted(maps.Keys(...)) to replace collect-keys-then-sort pattern#4954
pietern wants to merge 2 commits intogo1.21-slices-packagefrom
go1.23-sorted-map-keys

Conversation

@pietern
Copy link
Copy Markdown
Contributor

@pietern pietern commented Apr 13, 2026

Summary

  • Replace the 3-4 line "make slice, range map, append, sort" idiom with the Go 1.23 one-liner slices.Sorted(maps.Keys(...))
  • Remove utils.SortedKeys and two package-local duplicates (sortedKeys, sortKeys), inlining all call sites
  • Also replace slices.Collect(maps.Values(...)) + slices.Sort(...) with slices.Sorted(maps.Values(...)) in dynloc/locations.go

Test plan

  • go build ./... passes
  • Unit tests pass for all affected packages

This pull request was AI-assisted by Isaac.

… pattern

Go 1.23 added `slices.Sorted` which accepts an iterator and returns a
sorted slice. Combined with `maps.Keys`, this replaces the common
three-step pattern of allocating a slice, collecting keys, then sorting.

This removes the `utils.SortedKeys` helper and two package-local
duplicates (`sortedKeys`, `sortKeys`), inlining all call sites with
the stdlib one-liner.

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