aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEli Bendersky <eliben@golang.org>2023-06-12 09:00:26 -0700
committerGopher Robot <gobot@golang.org>2023-06-13 18:07:00 +0000
commit8faa79e91f52727fdf6c3fa2e96d1068a8b2f3df (patch)
tree5264453ed57710082be6f65ea85e7d898046a662
parent17dcbd866241c69990c23ce543ebd2906d546961 (diff)
downloadgo-8faa79e91f52727fdf6c3fa2e96d1068a8b2f3df.tar.gz
go-8faa79e91f52727fdf6c3fa2e96d1068a8b2f3df.zip
sort: comments directing new code to use the slices package when applicable
Change-Id: I0d4e902736fb3a75d128a088901055bece6c1a71 Reviewed-on: https://go-review.googlesource.com/c/go/+/502555 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Eli Bendersky <eliben@google.com> Auto-Submit: Eli Bendersky <eliben@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Eli Bendersky <eliben@google.com>
-rw-r--r--src/sort/sort.go21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/sort/sort.go b/src/sort/sort.go
index 68e2f0d082..1760e12c25 100644
--- a/src/sort/sort.go
+++ b/src/sort/sort.go
@@ -39,6 +39,9 @@ type Interface interface {
// Sort sorts data in ascending order as determined by the Less method.
// It makes one call to data.Len to determine n and O(n*log(n)) calls to
// data.Less and data.Swap. The sort is not guaranteed to be stable.
+//
+// Note: in many situations, the newer slices.SortFunc function is more
+// ergonomic and runs faster.
func Sort(data Interface) {
n := data.Len()
if n <= 1 {
@@ -96,6 +99,9 @@ func Reverse(data Interface) Interface {
}
// IsSorted reports whether data is sorted.
+//
+// Note: in many situations, the newer slices.IsSortedFunc function is more
+// ergonomic and runs faster.
func IsSorted(data Interface) bool {
n := data.Len()
for i := n - 1; i > 0; i-- {
@@ -154,23 +160,35 @@ func (x StringSlice) Sort() { Sort(x) }
// Convenience wrappers for common cases
// Ints sorts a slice of ints in increasing order.
+//
+// Note: consider using the newer slices.Sort function, which runs faster.
func Ints(x []int) { Sort(IntSlice(x)) }
// Float64s sorts a slice of float64s in increasing order.
// Not-a-number (NaN) values are ordered before other values.
+//
+// Note: consider using the newer slices.Sort function, which runs faster.
func Float64s(x []float64) { Sort(Float64Slice(x)) }
// Strings sorts a slice of strings in increasing order.
+//
+// Note: consider using the newer slices.Sort function, which runs faster.
func Strings(x []string) { Sort(StringSlice(x)) }
// IntsAreSorted reports whether the slice x is sorted in increasing order.
+//
+// Note: consider using the newer slices.IsSorted function, which runs faster.
func IntsAreSorted(x []int) bool { return IsSorted(IntSlice(x)) }
// Float64sAreSorted reports whether the slice x is sorted in increasing order,
// with not-a-number (NaN) values before any other values.
+//
+// Note: consider using the newer slices.IsSorted function, which runs faster.
func Float64sAreSorted(x []float64) bool { return IsSorted(Float64Slice(x)) }
// StringsAreSorted reports whether the slice x is sorted in increasing order.
+//
+// Note: consider using the newer slices.IsSorted function, which runs faster.
func StringsAreSorted(x []string) bool { return IsSorted(StringSlice(x)) }
// Notes on stable sorting:
@@ -204,6 +222,9 @@ func StringsAreSorted(x []string) bool { return IsSorted(StringSlice(x)) }
//
// It makes one call to data.Len to determine n, O(n*log(n)) calls to
// data.Less and O(n*log(n)*log(n)) calls to data.Swap.
+//
+// Note: in many situations, the newer slices.SortStableFunc function is more
+// ergonomic and runs faster.
func Stable(data Interface) {
stable(data, data.Len())
}