Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
Javascript: Hierarchical object sorting

I have an array of objects and every objects has the fields id, parentId and position. The position field stores the relative position of that item, so if I make a sort function like this:

object.sort((a, b) => {
  if(a.position < b. position) return -1;
  if(a.position > b. position) return 1;
  return 0;
})

It ends up like this:

{id: 1, parentId: null, position: 0}
{id: 3, parentId: null, position: 1}
{id: 5, parentId: 1, position: 1}
{id: 6, parentId: 3, position: 1}
{id: 4, parentId: null, position: 2}
{id: 2, parentId: 1, position: 2}

How to check both parentId and position to end up like this?

{id: 1, parentId: null, position: 0}
{id: 5, parentId: 1, position: 1}
{id: 2, parentId: 1, position: 2}
{id: 3, parentId: null, position: 1}
{id: 6, parentId: 3, position: 1}
{id: 4, parentId: null, position: 2}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could collect all nodes by their parentId and get the ordered result starting with null and by the ordered arrays.

const
    getNodes = parent => (parents[parent] || [])
        .sort((a, b) => a.position - b.position)
        .flatMap(o => [o, ...getNodes(o.id)]),
    data = [{ id: 1, parentId: null, position: 0 }, { id: 2, parentId: 1, position: 2 }, { id: 3, parentId: null, position: 1 }, { id: 6, parentId: 3, position: 1 }, { id: 4, parentId: null, position: 2 }, { id: 5, parentId: 1, position: 1 }],
    parents = data.reduce((r, o) => ((r[o.parentId] ??= []).push(o), r), {}),
    result = getNodes(null);

console.log(result);
console.log(parents);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda