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

mapsorter

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 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 each time

When can it be useful? Imagine, you have a map with the data and want to get slice of its keys that:

  • ordered of keys by string length, string value, datetime, int or float converted from strings or as native types;
  • ordered of values by string length, string value, datetime, int or float converted from strings or as native types;
  • in reverse order or only top N of the sorted results.

Not clear enough? See examples below.

Table of contents

Installation

go get github.com/mehanizm/mapsorter

Basic usage

You can see working examples in cmd/main.go or in the tests.

The lib has two different api. Please, use whichever is more convenient for you.

The functional style
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)
}
// >> 4
// >> 3
// >> 2
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.

The struct style
in := map[int]string{
	1: "a",
	2: "a",
	4: "c",
	3: "b",
}
sortedKeys, err := mapsorter.Map(in).ByValues().AsStringByLength().Reverse().Top(1).Sort()
if err != nil {
	panic(err)
}
for _, key := range sortedKeys {
	fmt.Println(key)
}
// >>  4

Or you can use MustSort() wrapper that panic if there is an internal error, be careful. But not so verbose.

for _, key := range mapsorter.Map(in).ByValues().AsStringByLength().Reverse().Top(1).MustSort() {
	fmt.Println(key)
}
// >>  4
Using options

All options can be changes with struct methods:

  • ByKeys() – sort by keys,
  • ByValues() – sort by values,
  • AsString() – sort as strings,
  • AsStringByLength() – sort as string lengths,
  • AsInt() – sort as ints,
  • AsFloat() – sort as floats,
  • AsDatetime() – sort as datetime (with smart conversion),
  • Forward() – forward order,
  • Reverse() – reverse order,
  • Top(top) – top N of sorted results,
  • All() – all results.

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 Map added in v0.0.2

func Map(mapToSort interface{}) *sorter

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