sortedmap

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 19, 2023 License: MIT Imports: 5 Imported by: 8 modules, 7 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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)

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyValuePair

type KeyValuePair[K constraints.Ordered, T any] struct {
	Key   K
	Value T
}

KeyValuePair describes an entry in SortedMap

func (KeyValuePair[K, T]) String

func (e KeyValuePair[K, T]) String() string

String implements the Stringer interface for KeyValuePair

type SortedByValue

type SortedByValue[K comparable, T constraints.Ordered] []ValueKeyPair[K, T]

SortedByValue is a structure that can sort a map[string]int by value

func AsSortedByValue

func AsSortedByValue[K comparable, T constraints.Ordered](m map[K]T, asc bool) (s SortedByValue[K, T])

AsSortedByValue return a SortedByValue from a map[string]int Note that this will panic if the input object is not a map string/int

func (SortedByValue[K, T]) Keys

func (s SortedByValue[K, T]) Keys() (keys []K)

Keys returns the list of keys for the entries in this SortedByValue

func (SortedByValue[K, T]) Len

func (s SortedByValue[K, T]) Len() int

func (SortedByValue[K, T]) Less

func (s SortedByValue[K, T]) Less(i, j int) bool

func (SortedByValue[K, T]) Sort

func (s SortedByValue[K, T]) Sort(asc bool)

Sort sorts a SortedByValue in ascending or descending order

func (SortedByValue[K, T]) Swap

func (s SortedByValue[K, T]) Swap(i, j int)

func (SortedByValue[K, T]) Values

func (s SortedByValue[K, T]) Values() (values []T)

Values returns the list of values for the entries in this SortedByValue

type SortedMap

type SortedMap[K constraints.Ordered, T any] []KeyValuePair[K, T]

SortedMap is a structure that can sort a map[string]type by key

func AsSortedMap

func AsSortedMap[K constraints.Ordered, T any](m map[K]T) (s SortedMap[K, T])

AsSortedMap return a SortedMap from a map[string]type. Note that this will panic if the input object is not a map

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

fmt.Println(AsSortedMap(unsorted))
Output:
["a": 1 "b": 2 "c": true "d": four "e": <nil>]

func NewSortedMap

func NewSortedMap() (s SortedMap[string, any])

NewSortedMap returns a SortedMap. Use the Add method to add elements and the Sort method to sort.

func NewSortedMapOf

func NewSortedMapOf[K constraints.Ordered, T any]() (s SortedMap[K, T])

NewSortedMapOf returns a SortedMap of string/T Use the Add method to add elements and the Sort method to sort.

func (SortedMap[K, T]) Add

func (s SortedMap[K, T]) Add(key K, value T) SortedMap[K, T]

Add adds an entry to a SortedMap (this require re-sorting the SortedMap when ready to display). Note that a SortedMap is internally a slice so you need to do something like:

s := NewSortedMap()
s = s.Add(key1, value1)
s = s.Add(key2, value2)

func (SortedMap[K, T]) Keys

func (s SortedMap[K, T]) Keys() (keys []K)

Keys returns the list of keys for the entries in this SortedMap

func (SortedMap[K, T]) Len

func (s SortedMap[K, T]) Len() int

func (SortedMap[K, T]) Less

func (s SortedMap[K, T]) Less(i, j int) bool

func (SortedMap[K, T]) MarshalJSON

func (s SortedMap[K, T]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface

func (SortedMap[K, T]) Sort

func (s SortedMap[K, T]) Sort()

Sort sorts a SortedMap (that should have probably be called SortableMap

func (SortedMap[K, T]) Swap

func (s SortedMap[K, T]) Swap(i, j int)

func (SortedMap[K, T]) Values

func (s SortedMap[K, T]) Values() (values []T)

Values returns the list of values for the entries in this SortedMap

type ValueKeyPair

type ValueKeyPair[K comparable, T constraints.Ordered] struct {
	Key   K
	Value T
}

ValueKeyPair describes an entry in SortedByValue

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址