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

178
Views
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 answers
Answer question

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 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!