tweego/sort.go
2020-02-24 16:08:26 -06:00

49 lines
896 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
Copyright © 20142020 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
}