Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

78
Views
How to sort an array by element nesting?

I have an array of data, I need to make sure that the elements are sorted by nesting (parent, child) and follow each other in alphabetical order both at the top level and at the nested one.

Initial data:

const arr = [
  {
    id: 0,
    name: 'A',
    parentId: null,
  },
  {
    id: 78,
    name: 'B',
    parentId: 77,
  },
  {
    id: 1715,
    name: 'C',
    parentId: 78,
  },
  {
    id: 77,
    name: 'D',
    parentId: null,
  },
  {
    id: 1716,
    name: 'E',
    parentId: 1715,
  },
  {
    id: 76,
    name: 'F',
    parentId: null,
  },
];

My approach doesn't work because it doesn't take into account additional nesting of elements.

function sortData() {
  const result = [];

  arr.sort((a, b) => {
    return a.name.localeCompare(b.name);
  });

  for (const element of arr) {
    if (!element.parentId) {
      const children = folders.filter((el) => el.parentId === element.id);

      result.push(element, ...children);
    }
  }

  return result;
}

What data do I need to get:

const arr = [
  {
    id: 0,
    name: 'A',
    parentId: null,
  },
  {
    id: 77,
    name: 'D',
    parentId: null,
  },
  {
    id: 78,
    name: 'B',
    parentId: 77,
  },
  {
    id: 1715,
    name: 'C',
    parentId: 78,
  },
  {
    id: 1716,
    name: 'E',
    parentId: 1715,
  },
  {
    id: 76,
    name: 'F',
    parentId: null,
  },
];

What data do I get using my approach:

const arr = [
  {
    id: 0,
    name: 'A',
    parentId: null,
  },
  {
    id: 77,
    name: 'D',
    parentId: null,
  },
  {
    id: 78,
    name: 'B',
    parentId: 77,
  },
  {
    id: 76,
    name: 'F',
    parentId: null,
  }
];
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

just use a simple sort function

const arr = [
  {
    id: 0,
    name: "A",
    parentId: null
  },
  {
    id: 78,
    name: "B",
    parentId: 77
  },
  {
    id: 1715,
    name: "C",
    parentId: 78
  },
  {
    id: 77,
    name: "D",
    parentId: null
  },
  {
    id: 1716,
    name: "E",
    parentId: 1715
  },
  {
    id: 76,
    name: "F",
    parentId: null
  }
];

arr.sort(function (a, b) {
if(b.parentId == a.id) return b;
  return a.id - b.id;
});

console.log('arr',arr)

about 4 years ago · Juan Pablo Isaza Report

0

It works on my side using a simple sort function

const arr = [
  {
    id: 0,
    name: 'A',
    parentId: null,
  },
  {
    id: 78,
    name: 'B',
    parentId: 77,
  },
  {
    id: 1715,
    name: 'C',
    parentId: 78,
  },
  {
    id: 77,
    name: 'D',
    parentId: null,
  },
  {
    id: 1716,
    name: 'E',
    parentId: 1715,
  },
  {
    id: 76,
    name: 'F',
    parentId: null,
  },
];

let result = []

const sortData = () => {
  arr.sort((a, b) => {
    return a.name.localeCompare(b.name);
  });
  return arr;
}

console.log(sortData())

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!