@@ -8,14 +8,16 @@ package slices
88
99import (
1010 "cmp"
11+ "internal/reflectlite"
1112 "math/bits"
1213)
1314
1415// Sort sorts a slice of any ordered type in ascending order.
1516// When sorting floating-point numbers, NaNs are ordered before other values.
1617func Sort [S ~ []E , E cmp.Ordered ](x S ) {
17- n := len (x )
18- pdqsortOrdered (x , 0 , n , bits .Len (uint (n )))
18+ sortSlice (x , func (i , j int ) bool {
19+ return cmp .Less (x [i ], x [j ])
20+ })
1921}
2022
2123// SortFunc sorts the slice x in ascending order as determined by the cmp
@@ -28,14 +30,36 @@ func Sort[S ~[]E, E cmp.Ordered](x S) {
2830// See https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings.
2931// The function should return 0 for incomparable items.
3032func SortFunc [S ~ []E , E any ](x S , cmp func (a , b E ) int ) {
31- n := len (x )
32- pdqsortCmpFunc (x , 0 , n , bits .Len (uint (n )), cmp )
33+ sortSlice (x , func (i , j int ) bool {
34+ return cmp (x [i ], x [j ]) < 0
35+ })
3336}
3437
3538// SortStableFunc sorts the slice x while keeping the original order of equal
3639// elements, using cmp to compare elements in the same way as [SortFunc].
3740func SortStableFunc [S ~ []E , E any ](x S , cmp func (a , b E ) int ) {
38- stableCmpFunc (x , len (x ), cmp )
41+ sortSliceStable (x , func (i , j int ) bool {
42+ return cmp (x [i ], x [j ]) < 0
43+ })
44+ }
45+
46+ type lessSwap struct {
47+ Less func (i , j int ) bool
48+ Swap func (i , j int )
49+ }
50+
51+ func sortSlice (x any , less func (i , j int ) bool ) {
52+ rv := reflectlite .ValueOf (x )
53+ swap := reflectlite .Swapper (x )
54+ length := rv .Len ()
55+ limit := bits .Len (uint (length ))
56+ pdqsort_func (lessSwap {less , swap }, 0 , length , limit )
57+ }
58+
59+ func sortSliceStable (x any , less func (i , j int ) bool ) {
60+ rv := reflectlite .ValueOf (x )
61+ swap := reflectlite .Swapper (x )
62+ stable_func (lessSwap {less , swap }, rv .Len ())
3963}
4064
4165// IsSorted reports whether x is sorted in ascending order.
0 commit comments