猿问

在 Golang 中,如何在不完全忽略大小写的情况下按字母顺序对字符串列表进行排序?

我希望字符串按字母顺序排序,并控制“A”是否在“a”之前。

在使用 strings.ToLower() 的 Less() 函数中无法实现这一点。有时“A”在“a”之前,有时在之后。


函数式编程
浏览 327回答 3
3回答

慕桂英3389331

不是使用 比较整个字符串strings.ToLower,而是比较单个符文。https://play.golang.org/p/RUMlmrb7C3gtype ByCase []stringfunc (s ByCase) Len() int&nbsp; &nbsp; &nbsp; { return len(s) }func (s ByCase) Swap(i, j int) { s[i], s[j] = s[j], s[i] }func (s ByCase) Less(i, j int) bool {&nbsp; &nbsp; iRunes := []rune(s[i])&nbsp; &nbsp; jRunes := []rune(s[j])&nbsp; &nbsp; max := len(iRunes)&nbsp; &nbsp; if max > len(jRunes) {&nbsp; &nbsp; &nbsp; &nbsp; max = len(jRunes)&nbsp; &nbsp; }&nbsp; &nbsp; for idx := 0; idx < max; idx++ {&nbsp; &nbsp; &nbsp; &nbsp; ir := iRunes[idx]&nbsp; &nbsp; &nbsp; &nbsp; jr := jRunes[idx]&nbsp; &nbsp; &nbsp; &nbsp; lir := unicode.ToLower(ir)&nbsp; &nbsp; &nbsp; &nbsp; ljr := unicode.ToLower(jr)&nbsp; &nbsp; &nbsp; &nbsp; if lir != ljr {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return lir < ljr&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // the lowercase runes are the same, so compare the original&nbsp; &nbsp; &nbsp; &nbsp; if ir != jr {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ir < jr&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // If the strings are the same up to the length of the shortest string,&nbsp;&nbsp; &nbsp; // the shorter string comes first&nbsp; &nbsp; return len(iRunes) < len(jRunes)}

SMILET

这可能是一个解决方案:package mainimport (&nbsp; &nbsp; "strings"&nbsp; &nbsp; "sort"&nbsp; &nbsp; "fmt")var listOfStrings []string = []string{&nbsp; &nbsp; "mars bar",&nbsp; &nbsp; "milk-duds",&nbsp; &nbsp; "Mars bar",&nbsp; &nbsp; "milk",&nbsp; &nbsp; "milky-way",&nbsp; &nbsp; "Milk",&nbsp; &nbsp; "Milky-way",&nbsp; &nbsp; "mars",}type Alphabetic []stringfunc (list Alphabetic) Len() int { return len(list) }func (list Alphabetic) Swap(i, j int) { list[i], list[j] = list[j], list[i] }func (list Alphabetic) Less(i, j int) bool {&nbsp; &nbsp; var si string = list[i]&nbsp; &nbsp; var sj string = list[j]&nbsp; &nbsp; var si_lower = strings.ToLower(si)&nbsp; &nbsp; var sj_lower = strings.ToLower(sj)&nbsp; &nbsp; if si_lower == sj_lower {&nbsp; &nbsp; &nbsp; &nbsp; return si < sj&nbsp; &nbsp; }&nbsp; &nbsp; return si_lower < sj_lower}func main() {&nbsp; &nbsp; fmt.Println("UNSORTED")&nbsp; &nbsp; printStrings(listOfStrings)&nbsp; &nbsp; sort.Sort(Alphabetic(listOfStrings))&nbsp; &nbsp; fmt.Println()&nbsp; &nbsp; fmt.Println("SORTED ALPHABETICALLY")&nbsp; &nbsp; printStrings(listOfStrings)}func printStrings(slice []string) {&nbsp; &nbsp; for i := 0; i < len(slice); i++ {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(slice[i])&nbsp; &nbsp; }}这是输出:UNSORTEDmars barmilk-dudsMars barmilkmilky-wayMilkMilky-waymarsSORTED ALPHABETICALLYmarsMars barmars barMilkmilkmilk-dudsMilky-waymilky-way

慕哥6287543

这是一个 Unicode 友好的方法,它利用strings.Map:package mainimport (&nbsp; &nbsp;"fmt"&nbsp; &nbsp;"sort"&nbsp; &nbsp;"strings"&nbsp; &nbsp;"unicode")type slice struct { sort.StringSlice }func (s slice) Less(d, e int) bool {&nbsp; &nbsp;t := strings.Map(unicode.ToUpper, s.StringSlice[d])&nbsp; &nbsp;u := strings.Map(unicode.ToUpper, s.StringSlice[e])&nbsp; &nbsp;return t < u}func main() {&nbsp; &nbsp;a := slice{&nbsp; &nbsp; &nbsp; sort.StringSlice{"a", "b", "A", "B"},&nbsp; &nbsp;}&nbsp; &nbsp;sort.Sort(a)&nbsp; &nbsp;fmt.Println(a.StringSlice) // [a A b B]}https://golang.org/pkg/strings#Map
随时随地看视频慕课网APP

相关分类

Go
我要回答