Stable sort for Array#sort.
See original GitHub issueNow that V8 has patched their Array#sort
to be stable (see here) it appears that Chakra may be one of the last (or the last) of the browser JS engines (JS Core, SpiderMonkey, V8, etc.) to perform a non-stable sort (see demo).
While not a spec violation, if all engines perform a stable sort we can work towards standardizing it in the TC39.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:7
- Comments:27 (19 by maintainers)
Top Results From Across the Web
Is Javascript Array.sort() Stable? | by Filip Sufitchi
There is no stable sort in the standard library. You're going to either need to use a 3rd party library, or implement it...
Read more >Array.prototype.sort() - JavaScript - MDN Web Docs
The sort() method sorts the elements of an array in place and returns the reference to the same array, now sorted. The default...
Read more >Stable Selection Sort
A sorting algorithm is said to be stable if two objects with equal or same keys appear in the same order in sorted...
Read more >Fast stable sorting algorithm implementation in javascript
According to the v8 dev blog and caniuse.com Array.sort is already stable as required by the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@dilijev idk, if we don’t have a situation in which we really want to use the native fallback behavior, it’s probably cleaner to just remove it now, imo - it’ll be in our git history if we need it, and historically off-by-default features tend to rot quickly in our codebase for various reasons. Getting a mergesort implementation in and unifying our supported platforms’ behavior would be good. In any case, if the spec were to eventually become even stricter in terms of what behavior is required of the standard array sort, then I imagine that it’d likely standardize on a merge sort - it’s significantly easier to reason about and work with consistently between engines. @rhuanjl, if you’d like to make a PR for a merge sort, I’m sure we’d be interested in looking at it.
This is not necessarily the case. Callback inlining would require we inline sort into the function that called it. In test cases calling sort with a single sort functions you should see regular inlining kick in, but in the general case, if sort is called with numerous different sort functions you wouldn’t get inlining unless sort is fully inlined into it’s caller.
@rhuanjl When you open a PR I’ll be happy to look at the perf and see what’s going wrong.