Documentation
¶
Overview ¶
Package sortmap allows for sorting maps by a custom comparator. For convenience, functions sorting by keys or values in ascending or descending order are provided – these can deal with limited types only, which are: bool, all built-in numerical types and string, time.Time.
Functions provided by this package panic when non-map type is passed for sorting or, in case of the key/value sorters, the underyling type is not supported.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Item ¶
type Item struct {
Key, Value interface{}
}
Item is a key-value pair representing element in the map
type Items ¶
type Items []Item
Items is a slice of map elements (key-value pairs)
func ByKey ¶
func ByKey(m interface{}) Items
ByKey sorts map by keys in the ascending order
Example ¶
m := map[string]int{"daikon": 2, "cabbage": 3, "banana": 1, "apple": 4}
for _, e := range sortmap.ByKey(m) {
fmt.Printf("%s\t%d\n", e.Key, e.Value)
}
Output: apple 4 banana 1 cabbage 3 daikon 2
func ByKeyDesc ¶
func ByKeyDesc(m interface{}) Items
ByKeyDesc sorts map by keys in the descending order
Example ¶
m := map[string]int{"daikon": 2, "cabbage": 3, "banana": 1, "apple": 4}
for _, e := range sortmap.ByKeyDesc(m) {
fmt.Printf("%s\t%d\n", e.Key, e.Value)
}
Output: daikon 2 cabbage 3 banana 1 apple 4
func ByValue ¶
func ByValue(m interface{}) Items
ByValue sorts map by values in the ascending order
Example ¶
m := map[string]int{"daikon": 2, "cabbage": 3, "banana": 1, "apple": 4}
for _, e := range sortmap.ByValue(m) {
fmt.Printf("%s\t%d\n", e.Key, e.Value)
}
Output: banana 1 daikon 2 cabbage 3 apple 4
func ByValueDesc ¶
func ByValueDesc(m interface{}) Items
ByValueDesc sorts map by values in the descending order
Example ¶
m := map[string]int{"daikon": 2, "cabbage": 3, "banana": 1, "apple": 4}
for _, e := range sortmap.ByValueDesc(m) {
fmt.Printf("%s\t%d\n", e.Key, e.Value)
}
Output: apple 4 cabbage 3 daikon 2 banana 1
