forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alphabetic.go
42 lines (32 loc) · 793 Bytes
/
alphabetic.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
package sorting
import (
"unicode"
)
type AlphabetSorter func([]string) func(i, j int) bool
// LessIgnoreCase returns true if first is alphabetically less than second.
func LessIgnoreCase(first string, second string) bool {
iRunes := []rune(first)
jRunes := []rune(second)
max := len(iRunes)
if max > len(jRunes) {
max = len(jRunes)
}
for idx := 0; idx < max; idx++ {
ir := iRunes[idx]
jr := jRunes[idx]
lir := unicode.ToLower(ir)
ljr := unicode.ToLower(jr)
if lir == ljr {
continue
}
return lir < ljr
}
return false
}
// SortAlphabeticFunc returns a `less()` comparator for sorting strings while
// respecting case.
func SortAlphabeticFunc(list []string) func(i, j int) bool {
return func(i, j int) bool {
return LessIgnoreCase(list[i], list[j])
}
}