I followed the documentation about sort
at https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort?v=example. It's not working. I even get a weirder issue: after hours, I discovered that if I change var comparator
to function comparator(a,b)
, it works. Why is that??
test();
var comparator = (a, b) => {
if (a.min !== b.min) {
if (a.min < b.min) {
return -1
} else {
return +1
}
} else {
if (a.max !== b.max) {
if (a.max < b.max) {
return -1
} else {
return +1
}
} else {
return 0;
}
}
}
function test() {
for (var x = 0; x < 1; x++) {
var tests = [{min: 18, max: 20},
{min: 17, max: 20},
{min: 10, max: 11}];
console.log(tests.sort(comparator), 'test sorted');
}
}
via TSR
No comments:
Post a Comment