In the sort callback, you receive two arguments (I usually call them a and b). You return a negative number if a should come before b, 0 if it doesn't matter (they're the same for sorting purposes), or a positive number if a should go after b.
In your case, since -1 goes at the end (you've said there are no other negative numbers), you just need to special-case it:
array.sort((a, b) => {
if (a === -1) { // < 0 would also work, since there aren't any others
return 1;
}
if (b === -1) { // "
return -1;
}
return a- b;
});
Live Example:
const array = [1, 85, -1, -1, 25, 0];
array.sort((a, b) => {
if (a === -1) {
return 1;
}
if (b === -1) {
return -1;
}
return a- b;
});
console.log(array);
That can be more concise, of course, I wrote it as above primarily for maximum clarity. But for instance:
array.sort((a, b) => a === -1 ? 1 : b === -1 ? -1 : a - b);
Personally I prefer slightly more verbose than that. But... :-)
-2?