sortx

package
v0.0.0-...-9d95335 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2024 License: MIT Imports: 3 Imported by: 1

README

intersort

https://github.com/lukechampine/intersort

intersort sorts slices of arbitrary objects according to the rules of Go's fmtsort package. fmtsort is an internal package and thus cannot be imported directly; however, its behavior is exposed via the fmt package when printing maps. So we can sort arbitrary objects by sticking them in a map, printing it, and parsing the result.

In other words, this package is an abomination and should not be used for anything. May the Go maintainers have mercy on my soul.

NOTE: This package requires Go 1.12.1.

Examples:

ints := []int{3, 1, 2}
intersort.Sort(ints) // [1, 2, 3]

strs := []string{"b", "c", "a"}
intersort.Sort(strs) // [a, b, c]

type Point struct {
    X, Y int
}
points := []Point{{2, 1}, {1, 1}, {1, 0}}
intersort.Sort(points) // [{1, 0}, {1, 1}, {2, 1}]

You can even sort differing types!

objs := []interface{}{3, true, 1, "wat", http.DefaultClient, false}
sort.Sort(intersort.Slice(objs)) // [false, true, 1, 3, wat, &{<nil> <nil> <nil> 0s}]

However, the results of this may vary, and in general are unpredictable; see https://github.com/golang/go/issues/30398.

Advance praise for intersort:

I tend to think that exposing the comparison function would be an attractive nuisance. - Ian Lance Taylor

It was a deliberate decision to keep this implementation private. - Rob Pike

Sorting [arbitrary objects] is not even possible in the general case. - cznic

This should not be done. - Rob Pike

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Sort

func Sort(slice interface{}, options ...SortOption)

Sort sorts arbitrary objects according to the map ordering of the fmt package.

Types

type LessSlice

type LessSlice struct {
	Slice
	Lesser
}

func WrapLesser

func WrapLesser(s Slice, lesser Lesser) LessSlice

func (LessSlice) Less

func (is LessSlice) Less(i, j int) bool

type Lesser

type Lesser func(a, b interface{}) bool

type Slice

type Slice []interface{}

Slice implements sort.Interface for arbitrary objects, according to the map ordering of the fmt package.

func (Slice) Len

func (is Slice) Len() int

func (Slice) Less

func (is Slice) Less(i, j int) bool

func (Slice) Swap

func (is Slice) Swap(i, j int)

type SortConfig

type SortConfig struct {
	Less Lesser
}

type SortOption

type SortOption func(*SortConfig)

func WithLess

func WithLess(f Lesser) SortOption

Jump to

Keyboard shortcuts

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