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

Function ExampleOption_sortedSlice

cmp/example_test.go:198–218  ·  cmp/example_test.go::ExampleOption_sortedSlice

Two slices may be considered equal if they have the same elements, regardless of the order that they appear in. Transformations can be used to sort the slice. This example is for demonstrative purposes; use [github.com/google/go-cmp/cmp/cmpopts.SortSlices] instead.

()

Source from the content-addressed store, hash-verified

196// This example is for demonstrative purposes;
197// use [github.com/google/go-cmp/cmp/cmpopts.SortSlices] instead.
198func ExampleOption_sortedSlice() {
199 // This Transformer sorts a []int.
200 trans := cmp.Transformer("Sort", func(in []int) []int {
201 out := append([]int(nil), in...) // Copy input to avoid mutating it
202 sort.Ints(out)
203 return out
204 })
205
206 x := struct{ Ints []int }{[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}
207 y := struct{ Ints []int }{[]int{2, 8, 0, 9, 6, 1, 4, 7, 3, 5}}
208 z := struct{ Ints []int }{[]int{0, 0, 1, 2, 3, 4, 5, 6, 7, 8}}
209
210 fmt.Println(cmp.Equal(x, y, trans))
211 fmt.Println(cmp.Equal(y, z, trans))
212 fmt.Println(cmp.Equal(z, x, trans))
213
214 // Output:
215 // true
216 // false
217 // false
218}
219
220type otherString string
221

Callers

nothing calls this directly

Calls 2

TransformerFunction · 0.92
EqualFunction · 0.92

Tested by

no test coverage detected