I need to sort an array of strings, but I need it so that null is always last. For example, the array:
var arr = [a, b, null, d, null]
When
Use a custom compare function that discriminates against null values:
null
arr.sort(function(a, b) { return (a===null)-(b===null) || +(a>b)||-(a
For descending order, just swap a and b in the direct comparison:
a
b
arr.sort(function(a, b) { return (a===null)-(b===null) || -(a>b)||+(a