Documentation ¶
Index ¶
- type KeyValuePair
- type SortedByValue
- type SortedMap
- func (s SortedMap[K, T]) Add(key K, value T) SortedMap[K, T]
- func (s SortedMap[K, T]) Keys() (keys []K)
- func (s SortedMap[K, T]) Len() int
- func (s SortedMap[K, T]) Less(i, j int) bool
- func (s SortedMap[K, T]) MarshalJSON() ([]byte, error)
- func (s SortedMap[K, T]) Sort()
- func (s SortedMap[K, T]) Swap(i, j int)
- func (s SortedMap[K, T]) Values() (values []T)
- type ValueKeyPair
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 ¶
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 ¶
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]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface
type ValueKeyPair ¶
type ValueKeyPair[K comparable, T constraints.Ordered] struct { Key K Value T }
ValueKeyPair describes an entry in SortedByValue