-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathREADME
170 lines (132 loc) · 5.56 KB
/
README
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
Sortutil is a Go library which lets you sort a slice without implementing a
sort.Interface, and in different orderings: ascending, descending, or
case-insensitive ascending or descending (for slices of strings.)
Additionally, Sortutil lets you sort a slice of a custom struct by a given
struct field or index--for example, you can sort a []MyStruct by the structs'
"Name" fields, or a [][]int by the second index of each nested slice, similar
to using sorted(key=operator.itemgetter/attrgetter) in Python.
== Installation
go get github.com/pmylund/sortutil
== Documentation
go doc github.com/pmylund/sortutil
or http://go.pkgdoc.org/github.com/pmylund/sortutil
== Functions
func Asc(slice interface{})
Sort a slice in ascending order.
func AscByField(slice interface{}, name string)
Sort a slice in ascending order by a field name.
func AscByFieldIndex(slice interface{}, index []int)
Sort a slice in ascending order by a list of nested field indices, e.g.
{1, 2, 3} to sort by the third field of the struct in the second field
of the struct in the first field of each struct in the slice.
func AscByIndex(slice interface{}, index int)
Sort a slice in ascending order by an index in a child slice.
func CiAsc(slice interface{})
Sort a slice in case-insensitive ascending order.
func CiAscByField(slice interface{}, name string)
Sort a slice in case-insensitive ascending order by a field name. (Valid
for string types.)
func CiAscByFieldIndex(slice interface{}, index []int)
Sort a slice in case-insensitive ascending order by a list of nested
field indices, e.g. {1, 2, 3} to sort by the third field of the struct
in the second field of the struct in the first field of each struct in
the slice. (Valid for string types.)
func CiAscByIndex(slice interface{}, index int)
Sort a slice in case-insensitive ascending order by an index in a child
slice. (Valid for string types.)
func CiDesc(slice interface{})
Sort a slice in case-insensitive descending order.
func CiDescByField(slice interface{}, name string)
Sort a slice in case-insensitive descending order by a field name.
(Valid for string types.)
func CiDescByFieldIndex(slice interface{}, index []int)
Sort a slice in case-insensitive descending order by a list of nested
field indices, e.g. {1, 2, 3} to sort by the third field of the struct
in the second field of the struct in the first field of each struct in
the slice. (Valid for string types.)
func CiDescByIndex(slice interface{}, index int)
Sort a slice in case-insensitive descending order by an index in a child
slice. (Valid for string types.)
func Desc(slice interface{})
Sort a slice in descending order.
func DescByField(slice interface{}, name string)
Sort a slice in descending order by a field name.
func DescByFieldIndex(slice interface{}, index []int)
Sort a slice in descending order by a list of nested field indices, e.g.
{1, 2, 3} to sort by the third field of the struct in the second field
of the struct in the first field of each struct in the slice.
func DescByIndex(slice interface{}, index int)
Sort a slice in descending order by an index in a child slice.
func Reverse(slice interface{})
Reverse a slice.
=== Utility functions for types that already implement sort.Interface
func ReverseInterface(s sort.Interface)
Reverse a type which implements sort.Interface.
func SortReverseInterface(s sort.Interface)
Sort a type using its existing sort.Interface, then reverse it. For a
slice with a "normal" sort interface (where Less returns true if i is
less than j), this causes the slice to be sorted in descending order.
== Examples
=== Normal sorting
ints := []int{4, 7, 2, 6}
// Sort the int slice in descending order, such that
// ints[0] == 7
// ints[1] == 6
// ints[2] == 4
// ints[3] == 2
sortutil.Desc(ints)
strings := []string{"ABC", "def", "abc", "GHI"}
// Sort the string slice in case-insensitive ascending order, such that:
// strings[0] == "ABC"
// strings[1] == "abc"
// strings[2] == "def"
// strings[3] == "GHI"
sortutil.CiAsc(strings)
=== Nested sorting
type MyStruct struct {
Id int
Name string
Date time.Time
}
now := time.Now()
day := 24*time.Hour
structs := []MyStruct{
{3, "foo", now.Add(1*day)},
{1, "bar", now.Add(-1*day)},
{2, "baz", now},
}
// Sort the slice by the Id field in ascending order, such that
// structs[0].Id == 1
// structs[1].Id == 2
// structs[2].Id == 3
sortutil.AscByField(structs, "Id")
// Sort the slice by the Date field in ascending order, such that
// structs[0].Date == yesterday
// structs[1].Date == now
// structs[2].Date == tomorrow
sortutil.AscByField(structs, "Date")
// Sort the slice by the Name field in descending order, such that
// structs[0].Name == "foo"
// structs[1].Name == "baz"
// structs[2].Name == "bar"
sortutil.DescByField(structs, "Name")
ints := [][]ints{
{4, 5, 1},
{2, 1, 7},
{9, 3, 3},
{1, 6, 2},
}
// Sort the ints by the last number in child slices in ascending
// order, such that
// ints[0] == {4, 5, 1}
// ints[1] == {1, 6, 2}
// ints[2] == {9, 3, 3}
// ints[3] == {2, 1, 7}
sortutil.AscByIndex(ints, 2)
== Performance
While sortutil is convenient, it won't beat a dedicated sort.Interface in
terms of performance. Implementing sort.Interface for a type ByName which
embeds e.g. []MyStruct and doing sort.Sort(ByName{MySlice}) should be
considered when high performance is required.
See the top of sortutil/all_test.go, go/src/pkg/sort/example_interface_test.go,
and go/src/pkg/sort/example_reverse_test.go for examples.