Giter Club home page Giter Club logo

mapsorter's Introduction

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

mapsorter's People

Contributors

mehanizm avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.