How to achieve Sorted HashMap

Written by
Link to Post by 

Go Lang newbie with some ruby experience here, saw other questions with simple data structure but mine is little different.

Asked the same thing on StackOverflow but like always for newbie got downvoted and flagged duplicate 😀

given hashmap like follows:

{ "1":{"a":0,"b":0,"c":0.025,"d":0}, "4":{"a":0,"b":0.025,"c":0,"d":0}, "3":{"a":0.025,"b":0,"c":0,"d":0}, "2":{"a":0,"b":0,"c":0,"d":0} } 

how do I sort in order of keys like:

{ "1":{"a":0,"b":0,"c":0.025,"d":0}, "2":{"a":0,"b":0,"c":0,"d":0}, "3":{"a":0.025,"b":0,"c":0,"d":0}, "4":{"a":0,"b":0.025,"c":0,"d":0} } 

tried the following:

var sortedData map[string]map[string]int for k := range sortedData { sortedData = append(sortedData, k) } sort.Strings(sortedData) 

but that resulted in errors like:

first argument to append must be slice; have map[string]map[string]intgo cannot use sortedData (type map[string]map[string]int) as type []string in argument to sort.Stringsgo 

submitted by /u/nirazan
[link] [comments]

Article Tags:
· ·
Article Categories:

Leave a Reply