Add msortv(), a stable, well-behaved sort method
Summary:
Ref T6861. We currently have three minor issues with sorting:
- If types get mixed, PHP goes crazy. This is discussed in T6861.
- The sort isn't guaranteed to be stable (identical input items are not reordered), but we'd probably always prefer a stable sort to a slightly more efficient one.
- Interacting with sort keys is a mess of sprintf('~%020.020f', ...) nonsense.
Introduce msortv() and PhutilSortVector to solve these. The new msortv() is:
- well-behaved (no craziness on mixed types); and
- stable (input items with the same sort key are never reordered).
PhutilSortVector is a helper so you don't have to do the sprintf(...) garbage. Instead, classes will do this:
- return sprintf( - '~%020d%s', - $this->getOrder(), - $this->getName()); + return id(new PhutilSortVector()) + ->addInt($this->getOrder()) + ->addString($this->getName());
This should be easier to read and much easier to get right, particularly for odd inputs at the edge of the range (like the most-negative integer).
Test Plan: Added and executed unit tests.
Reviewers: chad
Reviewed By: chad
Maniphest Tasks: T6861
Differential Revision: https://secure.phabricator.com/D15413