How to sort an array of objects based on the length of a nested array in JavaScript


This problem statement is saying to sort an array of objects based on the length of the nested arrays in it. For example if the first nested array has 3 elements and the second array has 2 elements then put the second array in first place and the first array at the second index.

Understanding the problem

Sorting an array of objects based on the size of a nested array is the task we have given. For example, let's say we have an array of objects in which each object has a name property and a hobbies property, which is an array of strings indicating the hobbies of that individual. The objective is to arrange the items in this array in such a way that the object with the shortest hobbies array is at the top of the sorted array and the object with the longest hobbies array is at the bottom.

We need to develop a special comparison method to address this issue, one that compares the hobbies array lengths of two objects and returns a value indicating which object should be placed first in the sorted array. The array may then be sorted based on the length of the hobbies arrays by calling the sort() method on the array and passing in this custom comparison function as an input. The sorted array may now be seen on the console.

Algorithm

The algorithm presented below will give a step-by-step procedure for fixing the problem.

Step 1: Use nested arrays to define an array of items in this step.

Step 2: Create the comparison function and add the sort() method.

Step 3: Use the length attribute of each object to compare the lengths of the nested arrays of the two objects inside the comparison method.

Step 4: Return -1 to indicate that the first item should come before the second item in the sorted array if the length of the first item's nested array is less than the length of the second object.

Step 5: If the length of the first item's nested array is greater than the length of the second object, return 1 to indicate that the second object should appear before the first object in the sorted array.

Step 6: Return 0 to demonstrate that if the lengths of the two items' nested arrays are equal, their places in the sorted array shouldn't be changed.

Step 7: Use the console.log() method to make the sorted array visible on the console.

Example

// define a function to check power of 3
const data = [
   { name: "John", hobbies: ["Reading", "dancing"] },
  { name: "Jane", hobbies: ["Cycling", "Singing", "Drawing"] },
  { name: "Bob", hobbies: ["Swimming"] },
  { name: "Alice", hobbies: [] }
];

// define sort method here
data.sort((a, b) => a.hobbies.length - b.hobbies.length);

console.log(data);

Other way of writing above code

// define a function to check power of 3
const data = [
   { name: "John", hobbies: ["Reading", "dancing"] },
  { name: "Jane", hobbies: ["Cycling", "Singing", "Drawing"] },
  { name: "Bob", hobbies: ["Swimming"] },
  { name: "Alice", hobbies: [] }
];

// define sort method here
data.sort((a, b) => {
  if (a.hobbies.length < b.hobbies.length) {
   return -1;
  }
  if (a.hobbies.length > b.hobbies.length) {
   return 1;
  }
  return 0;
});
console.log(data);

Output

[
  { name: 'Alice', hobbies: [] },
  { name: 'Bob', hobbies: [ 'Swimming' ] },
  { name: 'John', hobbies: [ 'Reading', 'dancing' ] },
  { name: 'Jane', hobbies: [ 'Cycling', 'Singing', 'Drawing' ] }
]

The sort() method is called on the array of created objects with nested arrays, along with a comparison function, in this programme. The length of the hobbies arrays of two objects, a and b, is compared via the comparison function. a appears before b in the sorted array if the length of a's hobbies array is less than b's hobbies array. In the sorted array, b comes before an if the length of the hobbies array for a is longer than the hobbies array for b. Their order in the sorted array does not change if the length of the hobbies arrays of a and b are equal. Next, the console receives a log of the sorted array.

Time complexity

The sort() method in JavaScript has an O(n * log n) time complexity, where n is the array's total number of entries. The time complexity may change depending on the size of the nested arrays since the sort() function is used in this scenario to sort an array of objects with nested arrays.

Because it executes a fixed number of operations for each comparison, the sort() method's comparison function has an O(1) time complexity. Consequently, assuming that the length of the nested arrays is negligibly tiny and has little impact on the efficiency of the sort() method, the overall time complexity of this programme is O(n * log n).

Conclusion

In this article, we have now covered how to sort an array of items in JavaScript according to the size of a nested array. This work has been accomplished using two different approaches, both of which use the sort() technique with a comparison function that assesses the length of the nested arrays. The comparison function's time complexity is O, but the sort() method's time complexity is O(n * log n) (1). The size of the nested arrays may affect the program's performance, but both approaches are effective for small to medium-sized arrays.

Updated on: 18-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements