使用sort包的Strings没有达到预期,有没有其他的方法可以对中文字符串数组排序?
sort.Strings排序默认是按照Unicode码点的顺序的, 如果需要按照拼音排序, 可以通过GBK转换实现, 自定义一个排序接口, 代码如下:
package main
import (
"bytes"
"fmt"
"io/ioutil"
"sort"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
//ByPinyin is customized sort interface to sort string by Chinese PinYin
type ByPinyin []string
func (s ByPinyin) Len() int { return len(s) }
func (s ByPinyin) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s ByPinyin) Less(i, j int) bool {
a, _ := UTF82GBK(s[i])
b, _ := UTF82GBK(s[j])
bLen := len(b)
for idx, chr := range a {
if idx > bLen-1 {
return false
}
if chr != b[idx] {
return chr < b[idx]
}
}
return true
}
//UTF82GBK : transform UTF8 rune into GBK byte array
func UTF82GBK(src string) ([]byte, error) {
GB18030 := simplifiedchinese.All[0]
return ioutil.ReadAll(transform.NewReader(bytes.NewReader([]byte(src)), GB18030.NewEncoder()))
}
//GBK2UTF8 : transform GBK byte array into UTF8 string
func GBK2UTF8(src []byte) (string, error) {
GB18030 := simplifiedchinese.All[0]
bytes, err := ioutil.ReadAll(transform.NewReader(bytes.NewReader(src), GB18030.NewDecoder()))
return string(bytes), err
}
func main() {
b := []string{"哈", "呼", "嚯", "ha", ","}
sort.Strings(b)
//output: [, ha 呼 哈 嚯]
fmt.Println("Default sort: ", b)
sort.Sort(ByPinyin(b))
//output: [, ha 哈 呼 嚯]
fmt.Println("By Pinyin sort: ", b)
}
实现Sort.Interface接口可以自定义排序顺序
type Interface interface {
// Len is the number of elements in the collection.
Len() int
// Less reports whether the element with
// index i should sort before the element with index j.
Less(i, j int) bool
// Swap swaps the elements with indexes i and j.
Swap(i, j int)
}
5 回答5.2k 阅读✓ 已解决
2 回答3k 阅读✓ 已解决
1 回答5k 阅读✓ 已解决
1 回答4.9k 阅读✓ 已解决
2 回答2.9k 阅读✓ 已解决
2 回答2.4k 阅读✓ 已解决
1 回答1.5k 阅读✓ 已解决
不行??