Documentation
¶
Overview ¶
Package sort provides primitives for sorting slices using golang generics
Index ¶
- func IntSlice(x []int)
- func IntSliceStable(x []int)
- func IsSorted[T any](data []T, less LessFunc)
- func Reverse[T any](data []T, less LessFunc) []T
- func Slice[T any](x []T, less LessFunc)
- func SliceStable[T any](x []T, less LessFunc)
- func Sort[T any](data []T, less LessFunc)
- func Stable[T any](data []T, less LessFunc)
- func StringSlice(x []string)
- func StringSliceStable(x []string)
- type LessFunc
- type SortableSlice
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IntSliceStable ¶
func IntSliceStable(x []int)
IntSlice stable sorts a slice of ints in increasing order.
func Slice ¶
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 ¶
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 ¶
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 ¶
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 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.