Categorygithub.com/gobs/sortedmap
modulepackage
1.0.0
Repository: https://github.com/gobs/sortedmap.git
Documentation: pkg.go.dev

# README

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)

# Functions

AsSortedByValue return a SortedByValue from a map[string]int Note that this will panic if the input object is not a map string/int.
AsSortedMap return a SortedMap from a map[string]type.
NewSortedMap returns a SortedMap.
NewSortedMapOf returns a SortedMap of string/T Use the Add method to add elements and the Sort method to sort.

# Structs

KeyValuePair describes an entry in SortedMap.
ValueKeyPair describes an entry in SortedByValue.

# Type aliases

SortedByValue is a structure that can sort a map[string]int by value.
SortedMap is a structure that can sort a map[string]type by key.