sort

package
v0.0.0-...-24ca9bf Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2022 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package sort provides a generic implementation of the standard library sort package primitives for sorting slices and user-defined collections.

It should be noted that even with generics, some limitations remain on operations such as sorting based on data type, e.g. While any Comparable type can be uniquely identified, used as map keys, or compared with operators, only Ordered types can be sorted.

Of the Go built-in types, the only Ordered types are ints, uints, uintptr, floats, and strings. Including user defined types in the sorting constraint infers that they must be ordered in some way. A user-defined interface is provided that must be implemented by any user-defined type in order for it to implement the algorithms in the sort package.

The basic interface implemented is:

 type Sorter interface {
		// Len is the number of elements in the collection.
		Len() int

		// Less reports whether the element with index i
		// must sort before the element with index j.
		// More details can be found in the standard
		// library sort package documentation ...
		Less(i, j int) bool

		// Swap swaps the elements with indexes i and j.
		Swap(i, j int)
	}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Float64s

func Float64s(x []float64)

Float64s sorts a slice of float64s in increasing order. Not-a-number (NaN) values are ordered before other values.

func Float64sAreSorted

func Float64sAreSorted(x []float64) bool

Float64sAreSorted reports whether the slice x is sorted in increasing order, with not-a-number (NaN) values before any other values.

func Ints

func Ints(x []int)

Ints sorts a slice of ints in increasing order.

func IntsAreSorted

func IntsAreSorted(x []int) bool

IntsAreSorted reports whether the slice x is sorted in increasing order.

func IsSorted

func IsSorted(data Interface) bool

IsSorted reports whether data is sorted.

func Sort

func Sort(data Interface)

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(data Interface)

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 Strings

func Strings(x []string)

Strings sorts a slice of strings in increasing order.

func StringsAreSorted

func StringsAreSorted(x []string) bool

StringsAreSorted reports whether the slice x is sorted in increasing order.

Types

type Float64Slice

type Float64Slice []float64

Float64Slice implements Interface for a []float64, sorting in increasing order, with not-a-number (NaN) values ordered before other values.

func (Float64Slice) Len

func (x Float64Slice) Len() int

func (Float64Slice) Less

func (x Float64Slice) Less(i, j int) bool

Less reports whether x[i] should be ordered before x[j], as required by the sort Interface. Note that floating-point comparison by itself is not a transitive relation: it does not report a consistent ordering for not-a-number (NaN) values. This implementation of Less places NaN values before any others, by using:

x[i] < x[j] || (math.IsNaN(x[i]) && !math.IsNaN(x[j]))

func (Float64Slice) Sort

func (x Float64Slice) Sort()

Sort is a convenience method: x.Sort() calls Sort(x).

func (Float64Slice) Swap

func (x Float64Slice) Swap(i, j int)

type IntSlice

type IntSlice []int

IntSlice attaches the methods of Interface to []int, sorting in increasing order.

func (IntSlice) Len

func (x IntSlice) Len() int

func (IntSlice) Less

func (x IntSlice) Less(i, j int) bool

func (IntSlice) Sort

func (x IntSlice) Sort()

Sort is a convenience method: x.Sort() calls Sort(x).

func (IntSlice) Swap

func (x IntSlice) Swap(i, j int)

type Interface

type Interface interface {
	// Len is the number of elements in the collection.
	Len() int

	// Less 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.
	Less(i, j int) bool

	// Swap swaps the elements with indexes i and j.
	Swap(i, j int)
}

An implementation of Interface can be sorted by the routines in this package. The methods refer to elements of the underlying collection by integer index.

func Reverse

func Reverse(data Interface) Interface

Reverse returns the reverse order for data.

type Sorter

type Sorter[K constraints.Ordered, V any] interface {
	// Len is the number of elements in the collection.
	Len() int

	// Less reports whether the element with index i
	// must sort before the element with index j.
	// More details can be found in the standard
	// library sort package documentation ...
	Less(i, j int) bool

	// Swap swaps the elements with indexes i and j.
	Swap(i, j int)
}

An implementation of the standard library sort.Interface that can be sorted by the routines in this package.

type StringSlice

type StringSlice []string

StringSlice attaches the methods of Interface to []string, sorting in increasing order.

func (StringSlice) Len

func (x StringSlice) Len() int

func (StringSlice) Less

func (x StringSlice) Less(i, j int) bool

func (StringSlice) Sort

func (x StringSlice) Sort()

Sort is a convenience method: x.Sort() calls Sort(x).

func (StringSlice) Swap

func (x StringSlice) Swap(i, j int)

Jump to

Keyboard shortcuts

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