Sort An Array With NULL Values Coming Last In JavaScript

To sort an array with NULL values coming last in JavaScript, use a sort function that supports comparing null values. Let’s see how to do this, in this post.

sort an array with null values coming last in JavaScript

Sort An Array With NULL Values Coming Last In JavaScript

The built-in Array.prototype.sort function does not support comparing null values by default. However, you can pass in a compareFunction that does support comparing null values.

The compareFunction should return:

  1. -1 if the first element is less than the second element
  2. 0 if the first element is equal to the second element
  3. 1 if the first element is greater than the second element

In addition, when comparing null values, the compareFunction should return 1 if the first element is null and the second element is not null. This will cause the NULL values to be sorted last.

The function should return -1 if the first element is not null and the second element is null, so that the NULL values are sorted last.

The function should return 0 if the first and the second element are both NULL.

Here is an example of how to use the sort function with a compareFunction that supports null values:

var myArray = ['a', 'b', null, 'c', null, 'd'];

myArray.sort(function(a, b){

if(a === null){

  return 1;

}

if(b === null){

  return -1;

}

if(a===b) return 0;

return a<b?-1:1;

});

console.log(myArray); // ['a', 'b', 'c', 'd', null, null]

As you can see from the code above, the compareFunction is able to compare NULL values. The function returns 1 when the first element is null and the second element is not null, so that the NULL values are sorted last.

The function returns -1 when the first element is not null and the second element is null, so that the NULL values are sorted last.

The function returns 0 when both elements are NULL, so that the order of the NULL values is preserved.

You can use this technique to sort an array with NULL values in any order you like.

For example, you could sort the array in reverse order by changing the return statement in the compareFunction to return 1 when the first element is less than the second element, and return -1 when the first element is greater than the second element.

var myArray = ['a', 'b', null, 'c', null, 'd'];

myArray.sort(function(a, b){

if(a === null){

  return 1;

}

if(b === null){

  return -1;

}

if(a===b) return 0;

return a>b?-1:1;

});

console.log(myArray); // ['d', 'c', 'b', 'a', null, null]

As you can see from the code above, the compareFunction is able to compare NULL values. The function returns 1 when the first element is null and the second element is not null, so that the NULL values are sorted last.

The function returns -1 when the first element is not null and the second element is null, so that the NULL values are sorted last.

The function returns 0 when both elements are NULL, so that the order of the NULL values is preserved.

Note that the sort() function changes the original array, and doesn’t return a new array.

If you do not want to mutate the original array, you can use the spread operator to create a new array:

var myArray = ['a', 'b', null, 'c', null, 'd'];

var newArray = [...myArray].sort(function(a, b){

if(a === null){

  return 1;

}

if(b === null){

  return -1;

}

if(a===b) return 0;

return a>b?-1:1;

});

console.log(myArray); // ['a', 'b', null, 'c', null, 'd']

console.log(newArray); // ['d', 'c', 'b', 'a', null, null]

As you can see from the code above, the new array is sorted in reverse order, with the NULL values coming last.

You can use this technique to sort an array with NULL values in ascending order as well –

Simply change the return statement in the compareFunction to return -1 when the first element is less than the second element, and return 1 when the first element is greater than the second element.

var myArray = ['a', 'b', null, 'c', null, 'd'];

var newArray = [...myArray].sort(function(a, b){

if(a === null){

  return 1;

}

if(b === null){

  return -1;

}

if(a===b) return 0;

return a<b?-1:1;

});

console.log(myArray); // ['a', 'b', null, 'c', null, 'd']

console.log(newArray); // ['a', 'b', 'c', 'd', null, null]

As you can see from the code above, the new array is sorted in ascending order, with the NULL values coming last.

Conclusion – Sort An Array With Null Values Coming Last In JavaScript

You can use the sort() function with a compareFunction that supports NULL values to sort an array with NULL values coming last in JavaScript. You can also use the spread operator to create a new array, which you can then sort without mutating the original array.

Leave a Reply