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

Function SortSlices

cmp/cmpopts/sort.go:36–43  ·  view source on GitHub ↗

SortSlices returns a [cmp.Transformer] option that sorts all []V. The lessOrCompareFunc function must be either a less function of the form "func(T, T) bool" or a compare function of the format "func(T, T) int" which is used to sort any slice with element type V that is assignable to T. A less func

(lessOrCompareFunc interface{})

Source from the content-addressed store, hash-verified

34//
35// SortSlices can be used in conjunction with [EquateEmpty].
36func SortSlices(lessOrCompareFunc interface{}) cmp.Option {
37 vf := reflect.ValueOf(lessOrCompareFunc)
38 if (!function.IsType(vf.Type(), function.Less) && !function.IsType(vf.Type(), function.Compare)) || vf.IsNil() {
39 panic(fmt.Sprintf("invalid less or compare function: %T", lessOrCompareFunc))
40 }
41 ss := sliceSorter{vf.Type().In(0), vf}
42 return cmp.FilterValues(ss.filter, cmp.Transformer("cmpopts.SortSlices", ss.sort))
43}
44
45type sliceSorter struct {
46 in reflect.Type // T

Callers 1

TestOptionsFunction · 0.85

Calls 5

IsTypeFunction · 0.92
FilterValuesFunction · 0.92
TransformerFunction · 0.92
IsNilMethod · 0.80
TypeMethod · 0.65

Tested by 1

TestOptionsFunction · 0.68