Trying to understand why
[{a: "Foo"}, {a: "Bar"}].sort(({a}) => (a === 'Bar' ? -1 : 0))
Works in Chrome (puts {a: “Bar”} in 1st position), but not in Node
Any pointers?
Workaround is defaulting to 1 instead of 0:
[{a: "Foo"}, {a: "Bar"}].sort(({a}) => (a === 'Bar' ? -1 : 1))
-
-
A good comparison function always handles all possible cases (-1, 0, +1). E.g. [{ a: 'Foo' }, { a: 'Bar' }].sort(({a}, {b}) => { if (a === 'Bar' && a === b) return 0; if (a === 'Bar') return -1; if (b === 'Bar') return 1; return 0; });
Thanks. Twitter will use this to make your timeline better. UndoUndo
-
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.
JavaScript, HTML, CSS, HTTP, performance, security, Bash, Unicode, i18n, macOS.
Previously, V8 used an unstable QuickSort for arrays with more than 10 elements. Now, we use the stable TimSort algorithm.
Demo: