MCPcopy
hub / github.com/google/go-cmp / SortKeys

Function SortKeys

cmp/internal/value/sort.go:16–32  ·  cmp/internal/value/sort.go::SortKeys

SortKeys sorts a list of map keys, deduplicating keys if necessary. The type of each value must be comparable.

(vs []reflect.Value)

Source from the content-addressed store, hash-verified

14// SortKeys sorts a list of map keys, deduplicating keys if necessary.
15// The type of each value must be comparable.
16func SortKeys(vs []reflect.Value) []reflect.Value {
17 if len(vs) == 0 {
18 return vs
19 }
20
21 // Sort the map keys.
22 sort.SliceStable(vs, func(i, j int) bool { return isLess(vs[i], vs[j]) })
23
24 // Deduplicate keys (fails for NaNs).
25 vs2 := vs[:1]
26 for _, v := range vs[1:] {
27 if isLess(vs2[len(vs2)-1], v) {
28 vs2 = append(vs2, v)
29 }
30 }
31 return vs2
32}
33
34// isLess is a generic function for sorting arbitrary map keys.
35// The inputs must be of the same type and must be comparable.

Callers 3

compareMapMethod · 0.92
FormatValueMethod · 0.92
TestSortKeysFunction · 0.92

Calls 1

isLessFunction · 0.85

Tested by 1

TestSortKeysFunction · 0.74