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

In JavaScript, you can sort an array of objects based on the length of nested arrays using the sort() method with a custom comparison function. This technique is useful when organizing data by array size.

Understanding the Problem

Consider an array of objects where each object contains a nested array property. To sort by nested array length, we need a comparison function that compares the length property of these nested arrays.

Sorting by Nested Array Length Before Sorting John: ["Reading", "dancing"] Length: 2 Jane: ["Cycling", "Singing", "Drawing"] Length: 3 Bob: ["Swimming"] Length: 1 Alice: [] Length: 0 sort() After Sorting Alice: [] Length: 0 Bob: ["Swimming"] Length: 1 John: ["Reading", "dancing"] Length: 2 Jane: ["Cycling", "Singing", "Drawing"] Length: 3

Using Simple Comparison (Recommended)

The most concise approach uses subtraction to compare array lengths:

const data = [
  { name: "John", hobbies: ["Reading", "dancing"] },
  { name: "Jane", hobbies: ["Cycling", "Singing", "Drawing"] },
  { name: "Bob", hobbies: ["Swimming"] },
  { name: "Alice", hobbies: [] }
];

// Sort by nested array length (ascending)
data.sort((a, b) => a.hobbies.length - b.hobbies.length);

console.log(data);
[
  { name: 'Alice', hobbies: [] },
  { name: 'Bob', hobbies: [ 'Swimming' ] },
  { name: 'John', hobbies: [ 'Reading', 'dancing' ] },
  { name: 'Jane', hobbies: [ 'Cycling', 'Singing', 'Drawing' ] }
]

Using Explicit Comparison Function

For more control, you can use an explicit comparison function that returns -1, 0, or 1:

const data = [
  { name: "John", hobbies: ["Reading", "dancing"] },
  { name: "Jane", hobbies: ["Cycling", "Singing", "Drawing"] },
  { name: "Bob", hobbies: ["Swimming"] },
  { name: "Alice", hobbies: [] }
];

data.sort((a, b) => {
  if (a.hobbies.length  b.hobbies.length) {
    return 1;   // b comes before a
  }
  return 0;     // equal, no change in order
});

console.log(data);
[
  { name: 'Alice', hobbies: [] },
  { name: 'Bob', hobbies: [ 'Swimming' ] },
  { name: 'John', hobbies: [ 'Reading', 'dancing' ] },
  { name: 'Jane', hobbies: [ 'Cycling', 'Singing', 'Drawing' ] }
]

Sorting in Descending Order

To sort from longest to shortest nested array, reverse the comparison:

const data = [
  { name: "John", hobbies: ["Reading", "dancing"] },
  { name: "Jane", hobbies: ["Cycling", "Singing", "Drawing"] },
  { name: "Bob", hobbies: ["Swimming"] },
  { name: "Alice", hobbies: [] }
];

// Sort by nested array length (descending)
data.sort((a, b) => b.hobbies.length - a.hobbies.length);

console.log(data);
[
  { name: 'Jane', hobbies: [ 'Cycling', 'Singing', 'Drawing' ] },
  { name: 'John', hobbies: [ 'Reading', 'dancing' ] },
  { name: 'Bob', hobbies: [ 'Swimming' ] },
  { name: 'Alice', hobbies: [] }
]

Comparison of Methods

Method Code Length Readability Performance
Simple Subtraction Short High Excellent
Explicit Comparison Longer Very High Good

Time Complexity

Both approaches have O(n log n) time complexity, where n is the number of objects in the array. The comparison function itself runs in O(1) constant time since it only accesses the length property.

Conclusion

Use simple subtraction (a.hobbies.length - b.hobbies.length) for sorting by nested array length. This approach is concise, readable, and performs efficiently for arrays of any size.

Updated on: 2026-03-15T23:19:00+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements