Black Lives Matter. Support the Equal Justice Initiative.

# Source file src/sort/slice.go

## Documentation: sort

```     1  // Copyright 2017 The Go Authors. All rights reserved.
2  // Use of this source code is governed by a BSD-style
4
5  package sort
6
7  // Slice sorts the slice x given the provided less function.
8  // It panics if x is not a slice.
9  //
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.
13  //
14  // The less function must satisfy the same requirements as
15  // the Interface type's Less method.
16  func Slice(x interface{}, less func(i, j int) bool) {
17  	rv := reflectValueOf(x)
18  	swap := reflectSwapper(x)
19  	length := rv.Len()
20  	quickSort_func(lessSwap{less, swap}, 0, length, maxDepth(length))
21  }
22
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.
26  //
27  // The less function must satisfy the same requirements as
28  // the Interface type's Less method.
29  func SliceStable(x interface{}, less func(i, j int) bool) {
30  	rv := reflectValueOf(x)
31  	swap := reflectSwapper(x)
32  	stable_func(lessSwap{less, swap}, rv.Len())
33  }
34
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 interface{}, less func(i, j int) bool) bool {
38  	rv := reflectValueOf(x)
39  	n := rv.Len()
40  	for i := n - 1; i > 0; i-- {
41  		if less(i, i-1) {
42  			return false
43  		}
44  	}
45  	return true
46  }
47
```

View as plain text