Help us shape the future of Go! Take the 2026 Go Developer Survey

mapsorter

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2021 License: MIT Imports: 7 Imported by: 0

README

Mapsorter

GoDoc Go codecov Go Report

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

  • ByKeys
  • ByValues

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 ❤️

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Sort

func Sort(mapToSortInterface interface{}, sortBy By,
	sortAs As, reverse bool, top int) (res []interface{}, err error)

Types

type As

type As uint8
const (
	AsString As = iota
	AsStringByLength
	AsInt
	AsFloat
	AsDatetime
)

type By

type By uint8
const (
	ByKeys By = iota
	ByValues
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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