-
-
Save gnsx/2c6293df6b16925bd18c0ffa6f6ca130 to your computer and use it in GitHub Desktop.
Go: sort a Map by key #golang
This file contains 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
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