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

150
Vistas
function that performs multisort is not resulting in sorted data

I'm trying to create a function that does multisorting so that the logic can be called multiple times within the code. However, when called, the results outputted to the console are not sorted - it is exactly the same as data that was passed into the function. I was able to get the code working before it became a separate method (see this post). Why isn't the function returning sorted data?

function multisort(data, sortBy) {
    const dataClone = JSON.parse(JSON.stringify(data));
    return dataClone.sort((a, b) => {
        let result = 0;
        sortBy.forEach(o => {
            if (result !== 0) return;
            const property = o.property;
            result = o.direction *
                (
                     a[property] > b[property] ? 1 :
                    (a[property] < b[property] ? -1 : 0)
                );
        });
        return result;
    });
}

const students = [
    {
        firstName: 'John',
        lastName: 'Appletree',
        grade: 12
    },
    {
        firstName: 'Mighty',
        lastName: 'Peachtree',
        grade: 10
    },
    {
        firstName: 'Kim',
        lastName: 'Appletree',
        grade: 11
    },
    {
        firstName: 'Shooter',
        lastName: 'Appletree',
        grade: 12
    },
    {
        firstName: 'Peter',
        lastName: 'Peachtree',
        grade: 12
    }
    ];

const sortBy = [
    {
      prop:'grade',
      direction: -1
    },
    {
      prop:'lastName',
      direction: 1
    }
];

const sortedStudents = multisort(students, sortBy);
console.log(students);
console.log(sortedStudents);

about 4 years ago · Juan Pablo Isaza
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