Mapsorter

Golang map sorter that frees you of writing boilerplate sorting code every time
Installation
go get github.com/mehanizm/mapsorter
Basic usage
You can see an example in cmd/main.go
in := map[int]string{
1: "a",
2: "a",
4: "c",
3: "b",
}
sortedKeys, err := mapsorter.Sort(in, mapsorter.ByKeys, mapsorter.AsInt, true, 3)
if err != nil {
panic(err)
}
for _, key := range sortedKeys {
fmt.Println(key)
}
Using options
Sort function has four extending options.
SortBy
SortAs
- AsString
- AsStringByLength
- AsInt
- AsFloat
- AsDatetime
These options are defined as enum package constants for easy using.
And two more:
reverse – bool flag to choose reverse sorting order if needed.
top – int count of top result to return.
Benchmarking
You can see some benchmark tests comparing mapsorter with straightforward boilerplate go code. Them shows approximately 5 times decreasing of all metrics.
But save time, planet and keys on your keyboard! :)
With love ❤️