/
main.go
59 lines (53 loc) · 1.37 KB
/
main.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
50
51
52
53
54
55
56
57
58
59
package main
import (
"bytes"
"fmt"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"io/ioutil"
"sort"
)
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{"330", "你", "a", "本来", ",", "y"}
//
// sort.Strings(b)
// //output: [, ha 呼 哈 嚯]
// fmt.Println("Default sort: ", b)
//
// sort.Sort(ByPinyin(b))
// //output: [, ha 哈 呼 嚯]
// fmt.Println("By Pinyin sort: ", b)
list := []uint{5, 1, 2, 7, 3}
sort.Slice(list, func(i, j int) bool {
return list[i] < list[j]
})
fmt.Println(list)
}