multisort

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2023 License: MIT Imports: 4 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CompareFn

type CompareFn[E any] func(a E, b E, descending bool) CompareResult

CompareFn is compare function that compares the two given values according to multisort criteria.

The function is implemented for comparable data types inside this package. Use it!

It returns: - Less when a is smaller than b (on descending: b is smaller than a) - NotEqual when a is not equal to b - 0 when neither less or not equal is returned

type CompareResult

type CompareResult int
const (
	Less     CompareResult = 1
	NotEqual CompareResult = 2
)

func Compare

func Compare[O constraints.Ordered](a O, b O, descending bool) CompareResult

Compare compares values according to multisort criteria.

func WithCompareFunc added in v0.10.2

func WithCompareFunc(f func() int, descending bool) CompareResult

WithCompareFunc compares input and returns a CompareResult according to multisort criteria.

type FieldMap

type FieldMap[E any] map[string]CompareFn[E]

FieldMap defines the fields that the sorter is capable to sort and provides the corresponsing compare funcs.

type Key

type Key struct {
	ID         string
	Descending bool
}

Key is the key that will be sorted by.

type Keys

type Keys []Key

type Sorter

type Sorter[E any] struct {
	// contains filtered or unexported fields
}

Sorter can sort by multiple criteria.

func New

func New[E any](fields FieldMap[E], defaultSortKeys Keys) *Sorter[E]

New creates a new multisorter.

func (*Sorter[E]) AvailableKeys

func (s *Sorter[E]) AvailableKeys() []string

AvailableKeys returns the available sort keys that this sorter has been initialized with.

func (*Sorter[E]) SortBy

func (s *Sorter[E]) SortBy(data []E, keys ...Key) error

SortBy sorts the given data by the given sort keys.

Jump to

Keyboard shortcuts

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