js number array sort bug All In One
js array default sort bug
// ⚠️ sort 会改变原始数组
arr = [2, -1,-2, 0];
// [2, -1, -2, 0]
arr.sort();
// [-1, -2, 0, 2] ❌
arr.sort((a, b) => a - b > 0);
// [-1, -2, 0, 2] ❌
arr.sort((a, b) => a - b > 0 ? 1 : -1);
// [-2, -1, 0, 2] ✅
arr;
// [-2, -1, 0, 2]