sort nested data using sort function javascript

Sorting Nested Data Using Sort Function in JavaScript

Sorting nested data using the sort function in JavaScript can be a bit tricky, but with the right knowledge and understanding of the language, it can be done with ease. In this article, we will explore the various ways of sorting nested data using the sort function in JavaScript.

Method 1: Sorting Arrays of Objects by a Single Property

One of the most common ways of sorting arrays of objects is by a single property. For example, if we have an array of objects that represent people and we want to sort them by their age, we can do the following:


const people = [
  { name: 'John', age: 25 },
  { name: 'Jane', age: 30 },
  { name: 'Bob', age: 20 },
];

// Sort by age
people.sort((a, b) => a.age - b.age);

console.log(people);
// Output: [
//   { name: 'Bob', age: 20 },
//   { name: 'John', age: 25 },
//   { name: 'Jane', age: 30 }
// ]

In this example, we use the sort function to sort the array of objects by the age property. The sort function takes a comparison function as an argument. This function compares two elements in the array and returns a negative value if the first element should come before the second, a positive value if the first element should come after the second, or zero if the two elements are equal.

Method 2: Sorting Arrays of Objects by Multiple Properties

Sometimes, we want to sort arrays of objects by multiple properties. For example, if we have an array of objects that represent books and we want to sort them by their author and then by their title, we can do the following:


const books = [
  { title: 'The Catcher in the Rye', author: 'J.D. Salinger' },
  { title: 'To Kill a Mockingbird', author: 'Harper Lee' },
  { title: 'The Great Gatsby', author: 'F. Scott Fitzgerald' },
  { title: 'Pride and Prejudice', author: 'Jane Austen' },
  { title: 'The Catcher in the Rye', author: 'Mark David Chapman' },
];

// Sort by author and then by title
books.sort((a, b) => {
  if (a.author < b.author) return -1;
  if (a.author > b.author) return 1;
  if (a.author === b.author) {
    if (a.title < b.title) return -1;
    if (a.title > b.title) return 1;
  }
  return 0;
});

console.log(books);
// Output: [
//   { title: 'The Catcher in the Rye', author: 'J.D. Salinger' },
//   { title: 'The Catcher in the Rye', author: 'Mark David Chapman' },
//   { title: 'The Great Gatsby', author: 'F. Scott Fitzgerald' },
//   { title: 'To Kill a Mockingbird', author: 'Harper Lee' },
//   { title: 'Pride and Prejudice', author: 'Jane Austen' }
// ]

In this example, we use the sort function to sort the array of objects first by the author property and then by the title property. We achieve this by returning different values in the comparison function depending on the values of the two objects being compared.

Method 3: Sorting Nested Arrays

Sometimes, we have nested arrays that we want to sort. For example, if we have an array of arrays that represent students and their grades and we want to sort the students by their average grade, we can do the following:


const students = [
  ['John', 95, 87, 92],
  ['Jane', 88, 91, 90],
  ['Bob', 91, 85, 89],
];

// Sort by average grade
students.sort((a, b) => {
  const avgA = a.slice(1).reduce((acc, grade) => acc + grade, 0) / (a.length - 1);
  const avgB = b.slice(1).reduce((acc, grade) => acc + grade, 0) / (b.length - 1);
  return avgB - avgA;
});

console.log(students);
// Output: [
//   ['John', 95, 87, 92],
//   ['Bob', 91, 85, 89],
//   ['Jane', 88, 91, 90]
// ]

In this example, we use the sort function to sort the array of arrays by the average grade of each student. We achieve this by calculating the average grade of each student and then returning a negative value if the first student should come before the second, a positive value if the first student should come after the second, or zero if the two students have the same average grade.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe