Created
July 11, 2018 08:52
-
-
Save LaughingVzr/7f11458997488f7f4c4ae35acd0939b2 to your computer and use it in GitHub Desktop.
[Golang实现自定义sort接口(根据结构值排序)] #golang #sort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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