I try to sort example array:
const array = [5, 9, NaN, 3, 15];
My expected result is:
const expected = [3, 5, 9, 15, NaN];
So the numbers are sorted ascending and the NaN
values are at the end.
I tried with simple
const res = array.sort((a, b) => a - b);
or
const res = array.sort((a, b) => {
if (a > b) return 1;
if (b > a) return -1;
return 0;
});
But none work. Side question – why the result is the same in both cases?
const array = [5, 9, NaN, 3, 15];
const res1 = array.sort((a, b) => a - b);
const res2 = array.sort((a, b) => b - a);
console.log(res1, res2);