OLD | NEW |
---|---|
(Empty) | |
1 package sortby | |
Vadim Sh.
2017/01/25 03:43:34
copyright
iannucci
2017/01/25 03:47:24
Done.
| |
2 | |
3 import ( | |
4 "sort" | |
5 "testing" | |
6 | |
7 . "github.com/smartystreets/goconvey/convey" | |
8 ) | |
9 | |
10 type CoolStruct struct { | |
11 A int | |
12 B int | |
13 C int | |
14 } | |
15 | |
16 type CoolStructSlice []CoolStruct | |
17 | |
18 func (s CoolStructSlice) Len() int { return len(s) } | |
19 func (s CoolStructSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | |
20 | |
21 // these could be defined inline inside of the Less function, too. | |
22 func (s CoolStructSlice) LessA(i, j int) bool { return s[i].A < s[j].A } | |
23 func (s CoolStructSlice) LessB(i, j int) bool { return s[i].B < s[j].B } | |
24 func (s CoolStructSlice) LessC(i, j int) bool { return s[i].C < s[j].C } | |
25 | |
26 func (s CoolStructSlice) Less(i, j int) bool { | |
27 return Chain{s.LessA, s.LessB, s.LessC}.Use(i, j) | |
28 } | |
29 | |
30 func TestSortBy(t *testing.T) { | |
31 t.Parallel() | |
32 | |
33 Convey("sortby", t, func() { | |
34 s := CoolStructSlice{ | |
35 {194, 771, 222}, | |
Vadim Sh.
2017/01/25 03:43:34
add some partially equal keys, to actually hit the
iannucci
2017/01/25 03:47:24
I just generated these with python, I'll add some
| |
36 {209, 28, 300}, | |
37 {413, 639, 772}, | |
38 {14, 761, 759}, | |
39 {866, 821, 447}, | |
40 {447, 373, 817}, | |
41 {132, 510, 149}, | |
42 {778, 513, 156}, | |
43 {713, 831, 596}, | |
44 {288, 83, 898}, | |
45 {679, 688, 903}, | |
46 {864, 100, 199}, | |
47 {229, 975, 648}, | |
48 {381, 290, 156}, | |
49 {311, 614, 434}, | |
50 } | |
51 | |
52 sort.Sort(s) | |
53 | |
54 So(s, ShouldResemble, CoolStructSlice{ | |
55 {14, 761, 759}, | |
56 {132, 510, 149}, | |
57 {194, 771, 222}, | |
58 {209, 28, 300}, | |
59 {229, 975, 648}, | |
60 {288, 83, 898}, | |
61 {311, 614, 434}, | |
62 {381, 290, 156}, | |
63 {413, 639, 772}, | |
64 {447, 373, 817}, | |
65 {679, 688, 903}, | |
66 {713, 831, 596}, | |
67 {778, 513, 156}, | |
68 {864, 100, 199}, | |
69 {866, 821, 447}, | |
70 }) | |
71 }) | |
72 } | |
OLD | NEW |