1
2
3
4
5
6
7 package internal
8
9 import (
10 "sort"
11
12 "golang.org/x/text/language"
13 )
14
15
16 func SortTags(tags []language.Tag) {
17 sort.Sort(sorter(tags))
18 }
19
20 type sorter []language.Tag
21
22 func (s sorter) Len() int {
23 return len(s)
24 }
25
26 func (s sorter) Swap(i, j int) {
27 s[i], s[j] = s[j], s[i]
28 }
29
30 func (s sorter) Less(i, j int) bool {
31 return s[i].String() < s[j].String()
32 }
33
34
35
36 func UniqueTags(tags []language.Tag) []language.Tag {
37 if len(tags) <= 1 {
38 return tags
39 }
40 SortTags(tags)
41 k := 0
42 for i := 1; i < len(tags); i++ {
43 if tags[k].String() < tags[i].String() {
44 k++
45 tags[k] = tags[i]
46 }
47 }
48 return tags[:k+1]
49 }
50
View as plain text