Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
GitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
sort: Stable merge is unstable if Less does not define a strict weak order #41951
The block merge portion of sort.Stable does not appear to be stable when a sequence contains unordered pairs of values (e.g. NaN < x).
Observe that the final 0 has migrated into the second NaN-bracketed portion.
Note that we are defining Less as
Perhaps the solution here is to document: "sort.Interface.Less must define a strict weak order, and float64 < float64 is not a strict weak order because it is not transitive (a < b and not c < b => a < c) for NaN values. Therefore do not use float64 < float64 for sorting."
C++ and Python's sort functions (std::stable_sort, sorted) are similarly unstable in the presence of NaN. Java's solution is to define a total order: java.lang.Double.compareTo is similar to Go's sort.Float64s but additionally orders -0.0 < 0.0.
- Describe requirements on Less more precisely. - Standardize on x for the variable name of the data being sorted (was variously a, p, slice). - Many other minor wording changes. Fixes #41951. Change-Id: Ic9e222a53ec035fcc3b5ddfc7f0eefbe1bb2890d Reviewed-on: https://go-review.googlesource.com/c/go/+/262657 Trust: Russ Cox <firstname.lastname@example.org> Run-TryBot: Russ Cox <email@example.com> Reviewed-by: Rob Pike <firstname.lastname@example.org>