-
Notifications
You must be signed in to change notification settings - Fork 8
/
sortutil.go
69 lines (52 loc) · 2.29 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
package sortutil
import (
"errors"
"sort"
"strings"
"time"
)
// For now, use only for slices < 100 in length for performance.
// To do: more scalable implementation that uses sorting/searching.
func InArrayStringCaseInsensitive(haystack []string, needle string) (string, error) {
needleLower := strings.ToLower(strings.TrimSpace(needle))
for _, canonical := range haystack {
canonicalLower := strings.ToLower(canonical)
if canonicalLower == needleLower {
return canonical, nil
}
}
return "", errors.New("String not found")
}
// Int64Slice attaches the methods of Interface to []int64, sorting in increasing order.
type Int32Slice []int32
func (p Int32Slice) Len() int { return len(p) }
func (p Int32Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p Int32Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// Sort is a convenience method.
func (p Int32Slice) Sort() { sort.Sort(p) }
// Int64Slice attaches the methods of Interface to []int64, sorting in increasing order.
type Int64Slice []int64
func (p Int64Slice) Len() int { return len(p) }
func (p Int64Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p Int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// Sort is a convenience method.
func (p Int64Slice) Sort() { sort.Sort(p) }
// Uint16Slice attaches the methods of Interface to []uint16, sorting in increasing order.
type Uint16Slice []uint16
func (p Uint16Slice) Len() int { return len(p) }
func (p Uint16Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p Uint16Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// Sort is a convenience method.
func (p Uint16Slice) Sort() { sort.Sort(p) }
type TimeSlice []time.Time
func (p TimeSlice) Len() int { return len(p) }
func (p TimeSlice) Less(i, j int) bool { return p[i].Before(p[j]) }
func (p TimeSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p TimeSlice) Sort() { sort.Sort(p) }
// Convenience wrappers for common cases
// Int64s sorts a slice of int64s in increasing order.
func Int32s(a []int32) { sort.Sort(Int32Slice(a)) }
// Int64s sorts a slice of int64s in increasing order.
func Int64s(a []int64) { sort.Sort(Int64Slice(a)) }
// Uint16s sorts a slice of uint16s in increasing order.
func Uint16s(a []uint16) { sort.Sort(Uint16Slice(a)) }