-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
sort.go
49 lines (38 loc) · 896 Bytes
/
sort.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
/*
Copyright © 2014–2020 Thomas Michael Edwards. All rights reserved.
Use of this source code is governed by a Simplified BSD License which
can be found in the LICENSE file.
*/
package main
import (
"unicode"
)
// StringsInsensitively provides for case insensitively sorting slices of strings.
type StringsInsensitively []string
func (p StringsInsensitively) Len() int {
return len(p)
}
func (p StringsInsensitively) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p StringsInsensitively) Less(i, j int) bool {
iRunes := []rune(p[i])
jRunes := []rune(p[j])
uBound := len(iRunes)
if uBound > len(jRunes) {
uBound = len(jRunes)
}
for pos := 0; pos < uBound; pos++ {
iR := iRunes[pos]
jR := jRunes[pos]
iRLo := unicode.ToLower(iR)
jRLo := unicode.ToLower(jR)
if iRLo != jRLo {
return iRLo < jRLo
}
if iR != jR {
return iR < jR
}
}
return false
}