aboutsummaryrefslogtreecommitdiff
path: root/src/sort
diff options
context:
space:
mode:
authorTom Levy <tomlevy93@gmail.com>2017-08-22 17:10:46 +1200
committerJoe Tsai <thebrokentoaster@gmail.com>2017-08-25 20:48:39 +0000
commit2bba267120ad8ce7e9ecd97f3ce4b08bce80b41b (patch)
tree3bd5bf913274ae453d29e0710f9e74714df4e45b /src/sort
parentaea286b449c5ffe6d69fb7bb0c5b05e480ed3ad9 (diff)
downloadgo-2bba267120ad8ce7e9ecd97f3ce4b08bce80b41b.tar.gz
go-2bba267120ad8ce7e9ecd97f3ce4b08bce80b41b.zip
sort: fix mix-up between "!less" and "greater" in examples
If Less(a, b) returns true when a is less than b, the correct way to check if a is greater than b is to use Less(b, a). It is wrong to use !Less(a, b) because that checks if a is greater than *or equal to* b. 1. The decreasingDistance function in Example_sortKeys makes this mistake. Fix it. 2. The documentation of multiSorter.Less says it loops along the less functions until it finds a comparison "that is either Less or !Less". This is nonsense, because (Less(a, b) or !Less(a, b)) is always true. Fix the documentation to say that it finds a comparison "that discriminates between the two items (one is less than the other)". The implementation already does this correctly. Change-Id: If52b79f68e4fdb0d1095edf29bdecdf154a61b8d Reviewed-on: https://go-review.googlesource.com/57752 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/sort')
-rw-r--r--src/sort/example_keys_test.go2
-rw-r--r--src/sort/example_multi_test.go9
2 files changed, 6 insertions, 5 deletions
diff --git a/src/sort/example_keys_test.go b/src/sort/example_keys_test.go
index a8e47e4926..648f919e68 100644
--- a/src/sort/example_keys_test.go
+++ b/src/sort/example_keys_test.go
@@ -73,7 +73,7 @@ func Example_sortKeys() {
return p1.distance < p2.distance
}
decreasingDistance := func(p1, p2 *Planet) bool {
- return !distance(p1, p2)
+ return distance(p2, p1)
}
// Sort the planets by the various criteria.
diff --git a/src/sort/example_multi_test.go b/src/sort/example_multi_test.go
index 40d12152ce..de6ec142d1 100644
--- a/src/sort/example_multi_test.go
+++ b/src/sort/example_multi_test.go
@@ -49,10 +49,11 @@ func (ms *multiSorter) Swap(i, j int) {
}
// Less is part of sort.Interface. It is implemented by looping along the
-// less functions until it finds a comparison that is either Less or
-// !Less. Note that it can call the less functions twice per call. We
-// could change the functions to return -1, 0, 1 and reduce the
-// number of calls for greater efficiency: an exercise for the reader.
+// less functions until it finds a comparison that discriminates between
+// the two items (one is less than the other). Note that it can call the
+// less functions twice per call. We could change the functions to return
+// -1, 0, 1 and reduce the number of calls for greater efficiency: an
+// exercise for the reader.
func (ms *multiSorter) Less(i, j int) bool {
p, q := &ms.changes[i], &ms.changes[j]
// Try all but the last comparison.