Skip to content
/ sortedmap Public

The sortedmap package provides some utility methods to sort maps by keys or values

License

Notifications You must be signed in to change notification settings

gobs/sortedmap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sortedmap

The sortedmap package provides some utility methods to sort maps by keys or values

See documentation at https://godoc.org/github.com/gobs/sortedmap

Usage

The most common use cases is to print the content of a map sorted by key:

unsorted := map[string]interface{}{
	"b": 2.0,
	"a": 1,
	"c": true,
	"e": nil,
	"d": "four",
}

fmt.Println(sortedmap.AsSortedMap(unsorted))	

// or

for _, ele := range sortedmap.AsSortedMap(unsorted) {
	fmt.Println(ele)    // implements Stringer
	
	fmt.Println(ele.Key + ":" + ele.Value)
}

You can also generate a JSON object with sorted keys:

jbuffer, _ := json.MarshalIndent(sortedmap.AsSortedMap(unsorted), "", "  ")
fmt.Println(string(jbuffer)

About

The sortedmap package provides some utility methods to sort maps by keys or values

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages