-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexamples_test.go
77 lines (60 loc) · 1.57 KB
/
examples_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package sorty
import (
"encoding/json"
"fmt"
)
func toJson(d interface{}) []byte {
js, _ := json.Marshal(d)
return js
}
func ExampleByKeys_Strings() {
s := NewSorter().ByKeys([]string{
"+foo",
"-bar",
})
data := []map[string]string{
{"foo": "abc", "bar": "xyz"},
{"foo": "xyz", "bar": "abc"},
{"foo": "def", "bar": "jhi"},
{"foo": "def", "bar": "def"},
{"foo": "mno", "bar": "jkl"},
}
s.Sort(data)
fmt.Printf("%s\n", toJson(data))
// Output:
// [{"bar":"xyz","foo":"abc"},{"bar":"jhi","foo":"def"},{"bar":"def","foo":"def"},{"bar":"jkl","foo":"mno"},{"bar":"abc","foo":"xyz"}]
}
func ExampleByKeys_interface() {
s := NewSorter().ByKeys([]string{
"foo",
"-bar",
})
data := []map[string]interface{}{
{"foo": "abc", "bar": 890},
{"foo": "xyz", "bar": 123},
{"foo": "def", "bar": 456},
{"foo": "mno", "bar": 789},
{"foo": "def", "bar": 789},
}
s.Sort(data)
fmt.Printf("%s\n", toJson(data))
// Output:
// [{"bar":890,"foo":"abc"},{"bar":789,"foo":"def"},{"bar":456,"foo":"def"},{"bar":789,"foo":"mno"},{"bar":123,"foo":"xyz"}]
}
func ExampleByKeyComps() {
s := NewSorter().ByKeyComps(KeyComps{
KeyComp{"foo", Ascending},
KeyComp{"bar", Descending},
})
data := []map[string]string{
{"foo": "abc", "bar": "xyz"},
{"foo": "xyz", "bar": "abc"},
{"foo": "def", "bar": "jhi"},
{"foo": "def", "bar": "def"},
{"foo": "mno", "bar": "jkl"},
}
s.Sort(data)
fmt.Printf("%s\n", toJson(data))
// Output:
// [{"bar":"xyz","foo":"abc"},{"bar":"jhi","foo":"def"},{"bar":"def","foo":"def"},{"bar":"jkl","foo":"mno"},{"bar":"abc","foo":"xyz"}]
}