Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda