Skip to content

Instantly share code, notes, and snippets.

@LaughingVzr
Created July 11, 2018 08:52
Show Gist options
  • Save LaughingVzr/7f11458997488f7f4c4ae35acd0939b2 to your computer and use it in GitHub Desktop.
Save LaughingVzr/7f11458997488f7f4c4ae35acd0939b2 to your computer and use it in GitHub Desktop.
[Golang实现自定义sort接口(根据结构值排序)] #golang #sort
package node
type node struct {
key string // 键名
count int // 总个数
}
type nodeList []node
func (nl nodeList) Len() int {
return len(nl)
}
func (nl nodeList) Less(i, j int) bool {
return nl[i].count > nl[j].count
}
func (nl nodeList) Swap(i, j int) {
nl[i], nl[j] = nl[j], nl[i]
}
// 排序匹配集合
func SortPatten(pt *map[string]int) nodeList {
var link []node
for key, value := range *pt {
link = append(link, node{key, value})
}
return nodeList(link)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment