github.com/c12o16h1/go/src@v0.0.0-20200114212001-5a151c0f00ed/sort/slice.go (about) 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. 4 5 package sort 6 7 // Slice sorts the provided slice given the provided less function. 8 // 9 // The sort is not guaranteed to be stable. For a stable sort, use 10 // SliceStable. 11 // 12 // The function panics if the provided interface is not a slice. 13 func Slice(slice interface{}, less func(i, j int) bool) { 14 rv := reflectValueOf(slice) 15 swap := reflectSwapper(slice) 16 length := rv.Len() 17 quickSort_func(lessSwap{less, swap}, 0, length, maxDepth(length)) 18 } 19 20 // SliceStable sorts the provided slice given the provided less 21 // function while keeping the original order of equal elements. 22 // 23 // The function panics if the provided interface is not a slice. 24 func SliceStable(slice interface{}, less func(i, j int) bool) { 25 rv := reflectValueOf(slice) 26 swap := reflectSwapper(slice) 27 stable_func(lessSwap{less, swap}, rv.Len()) 28 } 29 30 // SliceIsSorted tests whether a slice is sorted. 31 // 32 // The function panics if the provided interface is not a slice. 33 func SliceIsSorted(slice interface{}, less func(i, j int) bool) bool { 34 rv := reflectValueOf(slice) 35 n := rv.Len() 36 for i := n - 1; i > 0; i-- { 37 if less(i, i-1) { 38 return false 39 } 40 } 41 return true 42 }