Skip to content

Instantly share code, notes, and snippets.

@gnsx
Forked from flaviocopes/go-sort-map.go
Created February 15, 2019 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnsx/2c6293df6b16925bd18c0ffa6f6ca130 to your computer and use it in GitHub Desktop.
Save gnsx/2c6293df6b16925bd18c0ffa6f6ca130 to your computer and use it in GitHub Desktop.
Go: sort a Map by key #golang
import "sort"
ages := map[string]int{
"a": 1,
"c": 3,
"d": 4,
"b": 2,
}
names := make([]string, 0, len(ages))
for name := range ages {
names = append(names, name)
}
sort.Strings(names) //sort by key
for _, name := range names {
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment