package utils import "sort" type Pair struct { Key string Value int32 } type PairList []Pair func (p PairList) Len() int { return len(p) } func (p PairList) Less(i, j int) bool { return p[i].Value > p[j].Value } func (p PairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] } // https://stackoverflow.com/questions/18695346/how-can-i-sort-a-mapstringint-by-its-values func SortMapByValue(m map[string]int32) PairList { pl := make(PairList, len(m)) i := 0 for k, v := range m { pl[i] = Pair{k, v} i++ } sort.Sort(sort.Reverse(pl)) return pl }