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

180
Vistas
How to sort / re-arrange / shift JS array objects without changing original positioning

I have an array of

let arr = [
{workoutName : 'push-up'},
{workoutName : 'plank'},
{workoutName : 'single-Leg (Left)'},
{workoutName : 'arm-extend (Right)'},
{workoutName : 'Jumping Jack'},
{workoutName : 'single-Leg (Right)'},
{workoutName : 'something (Left)'},
{workoutName : 'arm-extend (Left)'},
{workoutName : 'somethingElse'},
{workoutName : 'something (Right)'}

]

I would like to sort / shift the original objects to match their pairs if available in this list and keep the positioning. Like shifting the names around without breaking the current structure.

This does sort it but I need to keep the same order.

let sorted = arr.sort( function( a , b){
    if(a.workoutName > b.workoutName) return 1;
    if(a.workoutName < b.workoutName) return -1;
    return 0;
});

console.log(sorted);
Array [ Object { workoutName: "Jumping Jack" }, 
    Object { workoutName: "arm-extend (Right)" }, 
    Object { workoutName: "arm-extend (Left)" }, 
    Object { workoutName: "plank" }, 
    Object { workoutName: "push-up" }, 
    Object { workoutName: "single-Leg (Left)" }, 
    Object { workoutName: "single-Leg (Right)" }, 
    Object { workoutName: "something (Left)" }, 
    Object { workoutName: "something (Right)" }, 
    Object { workoutName: "somethingElse" }
]

expected result

[
{workoutName : 'push-up'},
{workoutName : 'plank'},
{workoutName : 'single-Leg (Left)'},
{workoutName : 'single-Leg (Right)'},
{workoutName : 'arm-extend (Right)'},
{workoutName : 'arm-extend (Left)'},
{workoutName : 'Jumping Jack'},
{workoutName : 'something (Left)'},
{workoutName : 'something (Right)'}
{workoutName : 'somethingElse'},
]
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could group by the first part of workoutName and get grouped items in order as flat array.

const
    array = [{ workoutName: 'push-up' }, { workoutName: 'plank' }, { workoutName: 'single-Leg (Left)' }, { workoutName: 'arm-extend (Right)' }, { workoutName: 'Jumping Jack' }, { workoutName: 'single-Leg (Right)' }, { workoutName: 'something (Left)' }, { workoutName: 'arm-extend (Left)' }, { workoutName: 'somethingElse' }, { workoutName: 'something (Right)' }],
    order = { left: 1, right: 2 },
    sortReplace = s => s.replace(/^.*?(\((left|right)\))*$/i, (_, __, side = '') => order[side.toLowerCase()] || 0),
    sortSide = (a, b) => sortReplace(a.workoutName) - sortReplace(b.workoutName),
    getGroup = s => s.match(/.*?(?=(\s\(|$))/)?.[0] || '',
    result = Object
        .values(array.reduce((r, o) => {
            const group = getGroup(o.workoutName);
            if ((r[group] = r[group] || []).push(o) > 1) {
                r[group].sort(sortSide);
            }
            return r;
        }, {}))
        .flat();

console.log(result);
.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