-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
39 lines (34 loc) · 751 Bytes
/
Copy pathexample_test.go
File metadata and controls
39 lines (34 loc) · 751 Bytes
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
33
34
35
36
37
38
39
package keysort_test
import (
"fmt"
"github.com/itroot/keysort"
)
type Apple struct {
taste int
size int
good bool
}
type Orange struct {
taste int
size int
good bool
}
func Example() {
slice := []interface{}{&Apple{1, 1, true}, &Orange{5, 0, false}}
keysort.Sort(slice, func(i int) keysort.Sortable {
switch v := slice[i].(type) {
case *Apple:
return keysort.Sequence{keysort.BoolDesc(v.good), v.taste, v.size}
case *Orange:
return keysort.Sequence{keysort.BoolDesc(v.good), v.taste, v.size}
default:
panic("Unknown type")
}
})
for _, fruit := range slice {
fmt.Printf("%#v\n", fruit)
}
// Output:
// &keysort_test.Apple{taste:1, size:1, good:true}
// &keysort_test.Orange{taste:5, size:0, good:false}
}