- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStringCount.go
More file actions
Latest commit
74 lines (66 loc) · 1.72 KB
/
Copy pathStringCount.go
File metadata and controls
74 lines (66 loc) · 1.72 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package stringutil
import"sort"
// StringCount is a struct store count of Key
typeStringCountstruct {
Keystring
Countint
}
// StringCountList is slice of Keycount
typeStringCountList []StringCount
func (bStringCountList) Len() int { returnlen(b) }
func (bStringCountList) Less(i, jint) bool {
// return b[i].Count < b[j].Count
// This will return unwanted result: return b[i].Count < b[j].Count || b[i].Key < b[j].Key
ifb[i].Count<b[j].Count {
returntrue
}
ifb[i].Count==b[j].Count {
ifb[i].Key<b[j].Key {
returntrue
}
returnfalse
}
returnfalse
}
func (bStringCountList) Swap(i, jint) { b[i], b[j] =b[j], b[i] }
// ReversedStringCountList is Reversed StringCountList
typeReversedStringCountListstruct {
StringCountList
}
// Less is different from the Less of StringCountList
func (bReversedStringCountList) Less(i, jint) bool {
// return b.StringCountList[i].Count > b.StringCountList[j].Count
ifb.StringCountList[i].Count>b.StringCountList[j].Count {
returntrue
}
ifb.StringCountList[i].Count==b.StringCountList[j].Count {
ifb.StringCountList[i].Key<b.StringCountList[j].Key {
returntrue
}
returnfalse
}
returnfalse
}
// CountOfString returns the count of Key for a Key slice
funcCountOfString(s []string) map[string]int {
count:=make(map[string]int)
for_, b:=ranges {
count[b]++
}
returncount
}
// SortCountOfString sorts count of Key
funcSortCountOfString(countmap[string]int, reversebool) StringCountList {
countList:=make(StringCountList, len(count))
i:=0
forb, c:=rangecount {
countList[i] =StringCount{b, c}
i++
}
ifreverse {
sort.Sort(ReversedStringCountList{countList})
} else {
sort.Sort(countList)
}
returncountList
}