-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.go
32 lines (25 loc) · 949 Bytes
/
sort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package ordmap
import (
"cmp"
"slices"
"golang.org/x/exp/maps"
)
// Sort sorts the map by key using a custom comparison function and sets the indices accordingly.
func (om OrderedMap[K, V]) Sort(less func(K, K) int) {
SortFunc(om, setIndex, less)
}
// Sort is a helper function to sort a map by key and set the indices accordingly.
func Sort[M ~map[K]V, K cmp.Ordered, V any](m M, setIndex func(V, int) V) {
doSort(m, setIndex, slices.Sort)
}
// SortFunc is a helper function to sort a map by key using a custom comparison function and set the indices accordingly.
func SortFunc[M ~map[K]V, K comparable, V any](m M, setIndex func(V, int) V, less func(K, K) int) {
doSort(m, setIndex, func(keys []K) { slices.SortFunc(keys, less) })
}
func doSort[M ~map[K]V, K comparable, V any](m M, setIndex func(V, int) V, sortKeys func([]K)) {
keys := maps.Keys(m)
sortKeys(keys)
for i, key := range keys {
m[key] = setIndex(m[key], i+1)
}
}