Skip to content

Commit fbf62be

Browse files
adonovanianlancetaylor
authored andcommitted
sort: document requirements of Less relation
Fixes #34915 Change-Id: Ia62ff3b6f198ddcd79e8afc7b4f5514a44f2442c Reviewed-on: https://go-review.googlesource.com/c/go/+/261959 Reviewed-by: Rob Pike <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Akhil Indurti <[email protected]> Trust: Alberto Donizetti <[email protected]>
1 parent 9c56300 commit fbf62be

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/sort/sort.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ package sort
1111
// A type, typically a collection, that satisfies sort.Interface can be
1212
// sorted by the routines in this package. The methods require that the
1313
// elements of the collection be enumerated by an integer index.
14+
//
15+
// The sort routines require that the Less method implements a strict weak
16+
// ordering; see https://en.wikipedia.org/wiki/Weak_ordering.
17+
// The < operations on ints and strings are examples of such an ordering,
18+
// whereas the < operation on floating-point numbers is not, due to the
19+
// behavior of not-a-number (NaN) values.
1420
type Interface interface {
1521
// Len is the number of elements in the collection.
1622
Len() int
@@ -275,8 +281,9 @@ func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
275281
// Sort is a convenience method.
276282
func (p IntSlice) Sort() { Sort(p) }
277283

278-
// Float64Slice attaches the methods of Interface to []float64, sorting in increasing order
279-
// (not-a-number values are treated as less than other values).
284+
// Float64Slice attaches the methods of Interface to []float64, sorting in increasing order.
285+
// In order to satisfy the ordering requirements of the Less method, not-a-number (NaN)
286+
// values are treated as less than other values.
280287
type Float64Slice []float64
281288

282289
func (p Float64Slice) Len() int { return len(p) }

0 commit comments

Comments
 (0)