sort

package module
v0.0.0-...-2dfbb2e Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2022 License: MIT Imports: 1 Imported by: 1

README

slicesort

Package slicesort provides primitives for sorting slices using golang generics Go Version Go Reference License Report Card

Documentation

Overview

Package sort provides primitives for sorting slices using golang generics

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IntSlice

func IntSlice(x []int)

IntSlice sorts a slice of ints in increasing order.

func IntSliceStable

func IntSliceStable(x []int)

IntSlice stable sorts a slice of ints in increasing order.

func IsSorted

func IsSorted[T any](data []T, less LessFunc)

IsSorted reports whether data is sorted.

func Reverse

func Reverse[T any](data []T, less LessFunc) []T

Reverse returns the reverse order for data.

func Slice

func Slice[T any](x []T, less LessFunc)

Slice sorts the slice x using the provided less function.

The sort is not guaranteed to be stable: equal elements may be reversed from their original order. For a stable sort, use SliceStable.

The less function must satisfy the same requirements as the SortableSlice type's LessFunc.

func SliceStable

func SliceStable[T any](x []T, less LessFunc)

SliceStable sorts the slice x using the provided less function, keepint equal elements in their original order.

The less function must satisfy the same requirements as the SortableSlice type's LessFunc.

func Sort

func Sort[T any](data []T, less LessFunc)

Sort sorts data in ascending order as determined by the Less method. It makes one call to data.Len to determine n and O(n*log(n)) calls to data.Less and data.Swap. The sort is not guaranteed to be stable.

func Stable

func Stable[T any](data []T, less LessFunc)

Stable sorts data in ascending order as determined by the Less method, while keeping the original order of equal elements.

It makes one call to data.Len to determine n, O(n*log(n)) calls to data.Less and O(n*log(n)*log(n)) calls to data.Swap.

func StringSlice

func StringSlice(x []string)

StringSlice sorts a slice of strings in increasing order.

func StringSliceStable

func StringSliceStable(x []string)

StringSliceStable stable sorts a slice of strings in increasing order.

Types

type LessFunc

type LessFunc func(int, int) bool

LessFunc is the signature for the function to determine which element is the less.

type SortableSlice

type SortableSlice[T any] struct {
	// Data is the slice of any types.
	Data []T

	// LessFunc reports whether the element with index i
	// must sort before the element with index j.
	//
	// If both Less(i, j) and Less(j, i) are false,
	// then the elements at index i and j are considered equal.
	// Sort may place equal elements in any order in the final result,
	// while Stable preserves the original input order of equal elements.
	//
	// Less must describe a transitive ordering:
	//  - if both Less(i, j) and Less(j, k) are true, then Less(i, k) must be true as well.
	//  - if both Less(i, j) and Less(j, k) are false, then Less(i, k) must be false as well.
	//
	// Note that floating-point comparison (the < operator on float32 or float64 values)
	// is not a transitive ordering when not-a-number (NaN) values are involved.
	// See Float64Slice.Less for a correct implementation for floating-point values.
	LessFunc LessFunc
}

SortableSlice is an implementation of sort.Interface for a given slice.

func New

func New[T any](x []T, less LessFunc) *SortableSlice[T]

New will create a new SortableSlice.

func (SortableSlice[T]) Len

func (s SortableSlice[T]) Len() int

Len is the number of elements in the collection.

func (SortableSlice[T]) Less

func (s SortableSlice[T]) Less(i, j int) bool

Less will call LessFunc of SortableSlice.

func (SortableSlice[T]) Swap

func (s SortableSlice[T]) Swap(i, j int)

Swap swaps the elements with indexes i and j.

Jump to

Keyboard shortcuts

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