statefulsort

package
v0.0.0-...-7eebb46 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package statefulsort provides a sort interface to sort elements with a mutable order in which element fields are taken into account for sorting.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type StatefulSort

type StatefulSort struct {
	// contains filtered or unexported fields
}

StatefulSort implements sort.Interface and keeps track of the order in which the fields are looked at when comparing two elements.

func NewStatefulSort

func NewStatefulSort(e []interface{}, names []string, cmpFuncs []func(x, y interface{}) int) StatefulSort

NewStatefulSort returns a stateful sort for the slice of elements e, which must implement the Sortable interface. names is a slice of names corresponding to the fields of each element, and cmpFuncs is a slice of functions to compare two elements. The number of functions must be the same as the number of names, and each function compares based on the field corresponding to the name with the same index.

A comparison function returns -1 if x sorts before y, 0 if they sort the same, and 1 if y sorts before x, just like C's strcmp.

func (StatefulSort) Elements

func (s StatefulSort) Elements() []interface{}

Elements returns the elements of a stateful sort.

func (StatefulSort) Len

func (s StatefulSort) Len() int

Len returns the number of elements in s.

func (StatefulSort) Less

func (s StatefulSort) Less(i, j int) bool

Less compares two elements based on the current ordering of the comparison functions. If elements are not the same, true is returned if i sorts before j and false otherwise, unless reverse is set, in which case the opposite happens.

If the elements compare identical, the next sort function is considered.

func (*StatefulSort) SetPrimary

func (s *StatefulSort) SetPrimary(n string) error

SetPrimary moves the sort function corresponding to name n to the front of the sort function slice. If it already is at the front, the sort order is reversed by flipping reverse.

func (StatefulSort) Swap

func (s StatefulSort) Swap(i, j int)

Swap swaps the elements at indices i and j.

Jump to

Keyboard shortcuts

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