1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // Slice sorts the slice x given the provided less function.
8 // It panics if x is not a slice.
10 // The sort is not guaranteed to be stable: equal elements
11 // may be reversed from their original order.
12 // For a stable sort, use SliceStable.
14 // The less function must satisfy the same requirements as
15 // the Interface type's Less method.
16 func Slice(x any
, less
func(i
, j
int) bool) {
17 rv
:= reflectValueOf(x
)
18 swap
:= reflectSwapper(x
)
20 quickSort_func(lessSwap
{less
, swap
}, 0, length
, maxDepth(length
))
23 // SliceStable sorts the slice x using the provided less
24 // function, keeping equal elements in their original order.
25 // It panics if x is not a slice.
27 // The less function must satisfy the same requirements as
28 // the Interface type's Less method.
29 func SliceStable(x any
, less
func(i
, j
int) bool) {
30 rv
:= reflectValueOf(x
)
31 swap
:= reflectSwapper(x
)
32 stable_func(lessSwap
{less
, swap
}, rv
.Len())
35 // SliceIsSorted reports whether the slice x is sorted according to the provided less function.
36 // It panics if x is not a slice.
37 func SliceIsSorted(x any
, less
func(i
, j
int) bool) bool {
38 rv
:= reflectValueOf(x
)
40 for i
:= n
- 1; i
> 0; i
-- {