Why is this Array.sort behaviour different in Chrome vs Node.js
Problem Let's make a basic list and sort it to make sure that 2 is ALWAYS first in the list. Simple enough, right? [1, 2, 3].sort((a, b) => { if (a === 2) return -1; return 0; }); Chrome result: ✓ [2, 1, 3] Node result: X [1, 2, 3] In order to get this behaviour in Node, you could - weirdly enough - look at the b parameter and make it return 1 if it's 2: [1, 2, 3].sort((a, b) => { if (b === 2) return 1; return 0; }); With this implementation you get the opposite result; Chrome will be [1, 2, 3] and Node will be [2, 1, 3]. Questions Do you have a logical explaination for this behaviour? Is my s
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.