forked from patrickmn/sortutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortutil.go
369 lines (325 loc) · 10.9 KB
/
sortutil.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package sortutil
import (
"fmt"
"math"
"reflect"
"sort"
"strings"
"time"
)
// Ordering decides the order in which the specified data is sorted.
type Ordering int
func (o Ordering) String() string {
return orderings[o]
}
// A runtime panic will occur if case-insensitive is used when not sorting by
// a string type.
const (
Ascending Ordering = iota
Descending
CaseInsensitiveAscending
CaseInsensitiveDescending
)
var orderings = []string{
"Ascending",
"Descending",
"CaseInsensitiveAscending",
"CaseInsensitiveDescending",
}
// Recognized non-standard types
var (
t_time = reflect.TypeOf(time.Time{})
)
// A reflecting sort.Interface adapter.
type Sorter struct {
Slice reflect.Value
Getter Getter
Ordering Ordering
itemType reflect.Type // Type of items being sorted
vals []reflect.Value // Nested/child values that we're sorting by
valKind reflect.Kind
valType reflect.Type
}
// Sort the values in s.Slice by retrieving comparison items using
// s.Getter(s.Slice). A runtime panic will occur if s.Getter is not applicable
// to s.Slice, or if the values retrieved by s.Getter can't be compared, i.e.
// are unrecognized types.
func (s *Sorter) Sort() {
if s.Slice.Len() < 2 {
// Nothing to sort
return
}
if s.Getter == nil {
s.Getter = SimpleGetter()
}
s.itemType = s.Slice.Index(0).Type()
s.vals = s.Getter(s.Slice)
one := s.vals[0]
s.valType = one.Type()
s.valKind = one.Kind()
switch s.valKind {
// If the value isn't a standard kind, find a known type to sort by
default:
switch s.valType {
default:
panic(fmt.Sprintf("Cannot sort by type %v", s.valType))
case t_time:
switch s.Ordering {
default:
panic(fmt.Sprintf("Invalid ordering %v for time.Time", s.Ordering))
case Ascending:
sort.Sort(timeAscending{s})
case Descending:
sort.Sort(timeDescending{s})
}
}
// Strings
case reflect.String:
switch s.Ordering {
default:
panic(fmt.Sprintf("Invalid ordering %v for strings", s.Ordering))
case Ascending:
sort.Sort(stringAscending{s})
case Descending:
sort.Sort(stringDescending{s})
case CaseInsensitiveAscending:
sort.Sort(stringInsensitiveAscending{s})
case CaseInsensitiveDescending:
sort.Sort(stringInsensitiveDescending{s})
}
// Booleans
case reflect.Bool:
switch s.Ordering {
default:
panic(fmt.Sprintf("Invalid ordering %v for booleans", s.Ordering))
case Ascending:
sort.Sort(boolAscending{s})
case Descending:
sort.Sort(boolDescending{s})
}
// Ints
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
switch s.Ordering {
default:
panic(fmt.Sprintf("Invalid ordering %v for ints", s.Ordering))
case Ascending:
sort.Sort(intAscending{s})
case Descending:
sort.Sort(intDescending{s})
}
// Uints
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
switch s.Ordering {
default:
panic(fmt.Sprintf("Invalid ordering %v for uints", s.Ordering))
case Ascending:
sort.Sort(uintAscending{s})
case Descending:
sort.Sort(uintDescending{s})
}
// Floats
case reflect.Float32, reflect.Float64:
switch s.Ordering {
default:
panic(fmt.Sprintf("Invalid ordering %v for floats", s.Ordering))
case Ascending:
sort.Sort(floatAscending{s})
case Descending:
sort.Sort(floatDescending{s})
}
}
}
// Returns the length of the slice being sorted.
func (s *Sorter) Len() int {
return len(s.vals)
}
// Swaps two indices in the slice being sorted.
func (s *Sorter) Swap(i, j int) {
x := s.Slice.Index(i)
y := s.Slice.Index(j)
tmp := reflect.New(s.itemType).Elem()
tmp.Set(x)
x.Set(y)
y.Set(tmp)
s.vals[i], s.vals[j] = s.vals[j], s.vals[i]
}
// *cough* typedef *cough*
type stringAscending struct{ *Sorter }
type stringDescending struct{ *Sorter }
type stringInsensitiveAscending struct{ *Sorter }
type stringInsensitiveDescending struct{ *Sorter }
type boolAscending struct{ *Sorter }
type boolDescending struct{ *Sorter }
type intAscending struct{ *Sorter }
type intDescending struct{ *Sorter }
type uintAscending struct{ *Sorter }
type uintDescending struct{ *Sorter }
type floatAscending struct{ *Sorter }
type floatDescending struct{ *Sorter }
type timeAscending struct{ *Sorter }
type timeDescending struct{ *Sorter }
type reverser struct{ *Sorter }
func (s stringAscending) Less(i, j int) bool {
return s.Sorter.vals[i].String() < s.Sorter.vals[j].String()
}
func (s stringDescending) Less(i, j int) bool {
return s.Sorter.vals[i].String() > s.Sorter.vals[j].String()
}
func (s stringInsensitiveAscending) Less(i, j int) bool {
return strings.ToLower(s.Sorter.vals[i].String()) < strings.ToLower(s.Sorter.vals[j].String())
}
func (s stringInsensitiveDescending) Less(i, j int) bool {
return strings.ToLower(s.Sorter.vals[i].String()) > strings.ToLower(s.Sorter.vals[j].String())
}
func (s boolAscending) Less(i, j int) bool {
return !s.Sorter.vals[i].Bool() && s.Sorter.vals[j].Bool()
}
func (s boolDescending) Less(i, j int) bool {
return s.Sorter.vals[i].Bool() && !s.Sorter.vals[j].Bool()
}
func (s intAscending) Less(i, j int) bool { return s.Sorter.vals[i].Int() < s.Sorter.vals[j].Int() }
func (s intDescending) Less(i, j int) bool { return s.Sorter.vals[i].Int() > s.Sorter.vals[j].Int() }
func (s uintAscending) Less(i, j int) bool { return s.Sorter.vals[i].Uint() < s.Sorter.vals[j].Uint() }
func (s uintDescending) Less(i, j int) bool { return s.Sorter.vals[i].Uint() > s.Sorter.vals[j].Uint() }
func (s floatAscending) Less(i, j int) bool {
a := s.Sorter.vals[i].Float()
b := s.Sorter.vals[j].Float()
return a < b || math.IsNaN(a) && !math.IsNaN(b)
}
func (s floatDescending) Less(i, j int) bool {
a := s.Sorter.vals[i].Float()
b := s.Sorter.vals[j].Float()
return a > b || !math.IsNaN(a) && math.IsNaN(b)
}
func (s timeAscending) Less(i, j int) bool {
return s.Sorter.vals[i].Interface().(time.Time).Before(s.Sorter.vals[j].Interface().(time.Time))
}
func (s timeDescending) Less(i, j int) bool {
return s.Sorter.vals[i].Interface().(time.Time).After(s.Sorter.vals[j].Interface().(time.Time))
}
func (s reverser) Len() int {
return s.Sorter.Slice.Len()
}
// Unused--only to satisfy sort.Interface
func (s reverser) Less(i, j int) bool {
return i < j
}
// Returns a Sorter for a slice which will sort according to the
// items retrieved by getter, in the given ordering.
func New(slice interface{}, getter Getter, ordering Ordering) *Sorter {
v := reflect.ValueOf(slice)
return &Sorter{
Slice: v,
Getter: getter,
Ordering: ordering,
}
}
// Sort a slice using a Getter in the order specified by Ordering. getter
// may be nil if sorting a slice of a basic type where identifying a
// parent struct field or slice index isn't necessary, e.g. if sorting an
// []int, []string or []time.Time. A runtime panic will occur if getter is
// not applicable to the given data slice, or if the values retrieved by g
// cannot be compared.
func Sort(slice interface{}, getter Getter, ordering Ordering) {
New(slice, getter, ordering).Sort()
}
// Sort a slice in ascending order.
func Asc(slice interface{}) {
New(slice, nil, Ascending).Sort()
}
// Sort a slice in descending order.
func Desc(slice interface{}) {
New(slice, nil, Descending).Sort()
}
// Sort a slice in case-insensitive ascending order.
func CiAsc(slice interface{}) {
New(slice, nil, CaseInsensitiveAscending).Sort()
}
// Sort a slice in case-insensitive descending order.
func CiDesc(slice interface{}) {
New(slice, nil, CaseInsensitiveDescending).Sort()
}
// Sort a slice in ascending order by a field name.
func AscByField(slice interface{}, name string) {
New(slice, FieldGetter(name), Ascending).Sort()
}
// Sort a slice in descending order by a field name.
func DescByField(slice interface{}, name string) {
New(slice, FieldGetter(name), Descending).Sort()
}
// Sort a slice in case-insensitive ascending order by a field name.
// (Valid for string types.)
func CiAscByField(slice interface{}, name string) {
New(slice, FieldGetter(name), CaseInsensitiveAscending).Sort()
}
// Sort a slice in case-insensitive descending order by a field name.
// (Valid for string types.)
func CiDescByField(slice interface{}, name string) {
New(slice, FieldGetter(name), CaseInsensitiveDescending).Sort()
}
// Sort a slice in ascending order by a list of nested field indices, e.g. 1, 2,
// 3 to sort by the third field from the struct in the second field of the struct
// in the first field of each struct in the slice.
func AscByFieldIndex(slice interface{}, index []int) {
New(slice, FieldByIndexGetter(index), Ascending).Sort()
}
// Sort a slice in descending order by a list of nested field indices, e.g. 1, 2,
// 3 to sort by the third field from the struct in the second field of the struct
// in the first field of each struct in the slice.
func DescByFieldIndex(slice interface{}, index []int) {
New(slice, FieldByIndexGetter(index), Descending).Sort()
}
// 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 from the struct in the
// second field of the struct in the first field of each struct in the slice.
// (Valid for string types.)
func CiAscByFieldIndex(slice interface{}, index []int) {
New(slice, FieldByIndexGetter(index), CaseInsensitiveAscending).Sort()
}
// 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 from the struct in the
// second field of the struct in the first field of each struct in the slice.
// (Valid for string types.)
func CiDescByFieldIndex(slice interface{}, index []int) {
New(slice, FieldByIndexGetter(index), CaseInsensitiveDescending).Sort()
}
// Sort a slice in ascending order by an index in a child slice.
func AscByIndex(slice interface{}, index int) {
New(slice, IndexGetter(index), Ascending).Sort()
}
// Sort a slice in descending order by an index in a child slice.
func DescByIndex(slice interface{}, index int) {
New(slice, IndexGetter(index), Descending).Sort()
}
// Sort a slice in case-insensitive ascending order by an index in a child
// slice. (Valid for string types.)
func CiAscByIndex(slice interface{}, index int) {
New(slice, IndexGetter(index), CaseInsensitiveAscending).Sort()
}
// Sort a slice in case-insensitive descending order by an index in a child
// slice. (Valid for string types.)
func CiDescByIndex(slice interface{}, index int) {
New(slice, IndexGetter(index), CaseInsensitiveDescending).Sort()
}
// Reverse a slice.
func Reverse(slice interface{}) {
s := reverser{New(slice, nil, 0)}
if s.Len() < 2 {
return
}
s.itemType = s.Slice.Index(0).Type()
ReverseInterface(s)
}
// Reverse a type which implements sort.Interface.
func ReverseInterface(s sort.Interface) {
for i, j := 0, s.Len()-1; i < j; i, j = i+1, j-1 {
s.Swap(i, j)
}
}
// Sort a type using its existing sort.Interface, then reverse it. For a
// slice with a a "normal" sort interface (where Less returns true if i
// is less than j), this causes the slice to be sorted in descending order.
func SortReverseInterface(s sort.Interface) {
sort.Sort(s)
ReverseInterface(s)
}