Skip to content

Commit 5055bbc

Browse files
MaxGraeydcodeIO
authored andcommitted
Align portable Array#sort with stdlib (#879)
1 parent 74e957f commit 5055bbc

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

std/portable/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,24 @@ if (!String.prototype.replaceAll) {
206206
});
207207
}
208208

209+
function defaultComparator(a, b) {
210+
if (a === b) {
211+
if (a !== 0) return 0;
212+
a = 1 / a, b = 1 / b;
213+
} else {
214+
var nanA = a != a, nanB = b != b;
215+
if (nanA | nanB) return nanA - nanB;
216+
if (a == null) a = String(a);
217+
if (b == null) b = String(b);
218+
}
219+
return a > b ? 1 : -1;
220+
}
221+
222+
const arraySort = Array.prototype.sort;
223+
Array.prototype.sort = function sort(comparator) {
224+
return arraySort.call(this, comparator || defaultComparator);
225+
};
226+
209227
globalScope["isInteger"] = Number.isInteger;
210228

211229
globalScope["isFloat"] = function isFloat(arg) {

0 commit comments

Comments
 (0)