go语言中文字符串数组排序

180it 2020-10-26 PM 1829℃ 0条
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)
}
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

go语言中文字符串数组排序