Skip to content

Instantly share code, notes, and snippets.

@kylelemons
Created September 22, 2011 21:36
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save kylelemons/1236125 to your computer and use it in GitHub Desktop.
Save kylelemons/1236125 to your computer and use it in GitHub Desktop.
Sort a map by its values
package main
import "fmt"
import "sort"
func main() {
m := map[string]int{
"One": 1,
"Two": 2,
"Three": 3,
"Ten": 10,
"Fifty": 50,
}
vs := NewValSorter(m)
fmt.Printf("%v\n", *vs)
vs.Sort()
fmt.Printf("%v\n", *vs)
}
type ValSorter struct {
Keys []string
Vals []int
}
func NewValSorter(m map[string]int) *ValSorter {
vs := &ValSorter{
Keys: make([]string, 0, len(m)),
Vals: make([]int, 0, len(m)),
}
for k, v := range m {
vs.Keys = append(vs.Keys, k)
vs.Vals = append(vs.Vals, v)
}
return vs
}
func (vs *ValSorter) Sort() {
sort.Sort(vs)
}
func (vs *ValSorter) Len() int { return len(vs.Vals) }
func (vs *ValSorter) Less(i, j int) bool { return vs.Vals[i] < vs.Vals[j] }
func (vs *ValSorter) Swap(i, j int) {
vs.Vals[i], vs.Vals[j] = vs.Vals[j], vs.Vals[i]
vs.Keys[i], vs.Keys[j] = vs.Keys[j], vs.Keys[i]
}
{[Ten Fifty One Three Two] [10 50 1 3 2]}
{[One Two Three Ten Fifty] [1 2 3 10 50]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment