Sorting an array objects by property having null value in JavaScript


The problem statement says to perform sorting of an array of objects taking in one specific condition to sort null value key pairs present in the array of objects to be pushed at the end of the array where the array of objects is given by the user as an input source.

What is an Array in JavaScript ?

If you are familiar with any other programming language like C, C++, or Java, you must have heard the term 'array.'

In programming, an array is a collection of similar data elements under one roof.

Now, an important question arises: if arrays are generally the same in all languages, then how does JavaScript make arrays more unique and usable?

Let's understand the overall working of arrays in JavaScript.

An array is an object that stores multiple elements. Since an array is also an object, it has some properties and methods that make working with arrays easier in JavaScript.

Following is the syntax to define Arrays in JavaScript : −

Example

const arrayExample  = [ 2 , 3 , 5 ,6 ];
console.log(arrayExample);

Output

[2, 3, 5, 6]

What is an Array of Objects in Javascript ?

An array is an ordered collection of data whereas the objects are an unordered collection of data as objects for example represent the properties of a car , the array of objects represent specific properties of all types of car . Thus an array of objects is an ordered collection of more than one object inside it.

The syntax for array of objects looks like :

let studentArray = [
  {
    "name": "priya",
    "course": "JS",
    "from": "tutorialspoint",
    
  },
  {
    "name": "ashish",
    "course": "React",
    "from": "tutorialspoint",
  },
  {
    ...
  },
  ...
]

Algorithm

Step 1 − Declare a custom sorter function with name sorterOfNullValues taking in two values as its parameter to sort the values based on greater , lesser or equal parameters .

Step 2 − Create another helper function that decides the logic of custom sorting function to be called that decides sorting according to the problem statement such that if the value of the key equals to null , returns infinity which refers to a global object in javascript that returns the largest number present in the array of object given as an input else return the original value present in the original input.

Step 3 − Create a main function that calls the custom sort function on the original input array of objects given by the user .

Example

const sorterOfNullValues = (a, b) => {
      return assignValueOfNullAtEnd(a.value) - assignValueOfNullAtEnd(b.value);
   };
   
 const assignValueOfNullAtEnd = val => {
      if(val === null){
         return Infinity;
      }
      else{
         return val;
      };
   };
   
   
 function finalSorter(arr)
 {
    return  arr.sort(sorterOfNullValues);
 }
 
const arr  = [
  { name: 'eric', value: 1 },
  { name: 'bob', value: 4 },
  { name: 'michael', value: 0 },
  { name: 'john', value: 3 },
  { name: 'brad', value: null },
  { name: 'martin', value: 2 },
  { name: 'chris', value: null }
];
console.log(finalSorter(arr));

The particular code using above algorithm looks like this in console with respect to the problem statement :

Output

[
  { name: 'michael', value: 0 },
  { name: 'eric', value: 1 },
  { name: 'martin', value: 2 },
  { name: 'john', value: 3 },
  { name: 'bob', value: 4 },
  { name: 'brad', value: null },
  { name: 'chris', value: null }
]

Time and Space Complexity

Array.sort () method is based on the Time sort algorithm , giving the time complexity of O (n log n) and in the worst case , time complexity goes to O ( n^2).

Conclusion

This is how we can solve the above problem statement thinking logically and in the context of coding taking help of javascript methods and custom sort in its most efficient use case .

Updated on: 21-Aug-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements