-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsort_test.go
55 lines (49 loc) · 916 Bytes
/
sort_test.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package standard_library
import (
"sort"
"testing"
)
type Data struct {
Id int
Weights int
}
type DataArray []Data
func (d DataArray) Len() int {
return len(d)
}
func (d DataArray) Less(i, j int) bool {
return d[i].Weights > d[j].Weights
}
func (d DataArray) Swap(i, j int) {
d[i], d[j] = d[j], d[i]
}
func Test_sort(t *testing.T) {
data := DataArray{
{Id: 2, Weights: 2},
{Id: 1, Weights: 1},
{Id: 9, Weights: 9},
{Id: 8, Weights: 8},
{Id: 3, Weights: 3},
{Id: 6, Weights: 6},
{Id: 10, Weights: 10},
}
t.Log(data)
sort.Sort(data)
t.Log(data)
}
func Test_sortSlice(t *testing.T) {
data := []Data{
{Id: 2, Weights: 2},
{Id: 1, Weights: 1},
{Id: 9, Weights: 9},
{Id: 8, Weights: 8},
{Id: 3, Weights: 3},
{Id: 6, Weights: 6},
{Id: 10, Weights: 10},
}
t.Log(data)
sort.Slice(data, func(i, j int) bool {
return data[i].Weights < data[j].Weights
})
t.Log(data)
}