JOJ3/pkg/utils/map.go
张泊明518370910136 4b848bf1f1
Some checks failed
build / trigger-build-image (push) Blocked by required conditions
build / build (push) Has been cancelled
submodules sync / sync (push) Has been cancelled
feat(parser/cpplint): better summary
2024-11-01 21:21:15 -04:00

20 lines
387 B
Go

package utils
import "sort"
type Pair[K comparable, V any] struct {
Key K
Value V
}
func SortMap[K comparable, V any](m map[K]V, less func(i, j Pair[K, V]) bool) []Pair[K, V] {
pairs := make([]Pair[K, V], 0, len(m))
for k, v := range m {
pairs = append(pairs, Pair[K, V]{k, v})
}
sort.Slice(pairs, func(i, j int) bool {
return less(pairs[i], pairs[j])
})
return pairs
}