sortmap

package module
v0.0.0-...-4b9ddc7 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2019 License: MIT Imports: 4 Imported by: 11

README

gosortmap GoDoc Build Status

Sort maps in Go by keys or values. Works with most built-in types; own comparator can be provided to support custom types and ordering.

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

fmt.Println(sortmap.ByValueDesc(m).Top(2))
// Output:
// [{apple 4} {cabbage 3}]

Benchmark

This package favors convenience over the speed, so if the latter is preferable, you should go with an intermediate structure implementing sort.Interface and use sort.Sort directly. Here are the results for sorting map[int]int with 1000 values, indicating manual sorting can be up to 2x faster:

BenchmarkByKey-8           	     300	   4094204 ns/op
BenchmarkByKey_manual-8    	     500	   2367496 ns/op

BenchmarkByFunc-8          	     500	   3623426 ns/op
BenchmarkByFunc_manual-8   	    1000	   2122365 ns/op

(go 1.11.2, i7-6820HQ)

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 ByFunc

func ByFunc(m interface{}, c Less) Items

ByFunc sorts map using a provided comparator

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

func (Items) Top

func (r Items) Top(n int) Items

Top returns slice of up to n leading elements

type Less

type Less func(x, y Item) bool

Less compares two map elements and returns true if x < y

Jump to

Keyboard shortcuts

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