github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/sortx/README.md (about) 1 # intersort 2 3 https://github.com/lukechampine/intersort 4 5 `intersort` sorts slices of arbitrary objects according to the rules of Go's 6 `fmtsort` package. `fmtsort` is an internal package and thus cannot be imported 7 directly; however, its behavior is exposed via the `fmt` package when printing 8 maps. So we can sort arbitrary objects by sticking them in a map, printing it, 9 and parsing the result. 10 11 In other words, this package is an abomination and should not be used for 12 anything. May the Go maintainers have mercy on my soul. 13 14 **NOTE: This package requires Go 1.12.1.** 15 16 ## Examples: 17 18 ```go 19 ints := []int{3, 1, 2} 20 intersort.Sort(ints) // [1, 2, 3] 21 22 strs := []string{"b", "c", "a"} 23 intersort.Sort(strs) // [a, b, c] 24 25 type Point struct { 26 X, Y int 27 } 28 points := []Point{{2, 1}, {1, 1}, {1, 0}} 29 intersort.Sort(points) // [{1, 0}, {1, 1}, {2, 1}] 30 ``` 31 32 You can even sort *differing* types! 33 34 ```go 35 objs := []interface{}{3, true, 1, "wat", http.DefaultClient, false} 36 sort.Sort(intersort.Slice(objs)) // [false, true, 1, 3, wat, &{<nil> <nil> <nil> 0s}] 37 ``` 38 39 However, the results of this may vary, and in general are unpredictable; see 40 https://github.com/golang/go/issues/30398. 41 42 43 ## Advance praise for intersort: 44 45 > I tend to think that exposing the comparison function would be an attractive nuisance. - Ian Lance Taylor 46 47 > It was a deliberate decision to keep this implementation private. - Rob Pike 48 49 > Sorting [arbitrary objects] is not even possible in the general case. - cznic 50 51 > This should not be done. - Rob Pike