question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stable sort for Array#sort.

See original GitHub issue

Now 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).

https://github.com/Microsoft/ChakraCore/blob/0f40413d2e29863edf5f82de14fac41843deb79f/lib/Runtime/Library/JavascriptArray.cpp#L6640-L6675

While not a spec violation, if all engines perform a stable sort we can work towards standardizing it in the TC39.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:27 (19 by maintainers)

github_iconTop GitHub Comments

2reactions
Penguinwizzardcommented, Sep 19, 2018

@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.

0reactions
sigatrevcommented, Sep 21, 2018

If the user provides a simple callback for the comparison function, that should get inlined into the sort impl regardless due to normal inlining heuristics.

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.

Read more comments on GitHub >

github_iconTop 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 Array.prototype.sort
Array.prototype.sort is now guaranteed to be stable.
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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found