Skip to content

lukechampine/intersort

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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 golang/go#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

About

Sort anything.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages