-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Closed
Labels
Milestone
Description
There are helper functions to extract the keys and the values, but not one to extract both together.
I propose adding:
type Entry[K comparable, V any] struct {
K K
V V
}
func Entries[M ~map[K]V, K comparable, V any](m M) []Entry[K, V] {
r := make([]Entry[K, V], 0, len(m))
for k, v := range m {
r = append(r, Entry[K, V]{k, v})
}
return r
}I often find myself sorting a map based on the value, but needing the key and value paired together.
This API would ease that pattern:
entries := maps.Entries(m)
slices.SortFunc(entries, func(x, y maps.Entry[K, V]) bool {
return x.V < y.V
})mdlayher, komuw, j178, icholy, vadyus and 9 more