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

228
Views
Sorting within another sort

I have a set of data that has multiple properties, and the primary way I want them sorted is having item B after item A, but I don't want to affect the order of those items outside of that.

Dataset (in no particular order):

  • record 1
  • record 2
  • record 3 (has Item A property)
  • record 4
  • record 5 (item A)

Dataset (sorted by item A)

  • record 1
  • record 2
  • record 4
  • record 5 (item A)
  • record 3 (has Item A property)

My goal is to sort whatever has the "item A property" after the actual "item A". I'm blanking on how to make sure it's always sorted below the other item, regardless of what order each item returns.

I'm trying to do this within a JavaScript sort function, if possible.

Here's another example as well:

Dataset (in no particular order):

  • record 1
  • record 2 (item A)
  • record 3
  • record 4
  • record 5 (has Item A property)

Dataset (sorted by item A)

  • record 1
  • record 2 (item A)
  • record 5 (has Item A property)
  • record 3
  • record 4
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could map the items and for the wanted group take an array. later flat the result.

const
    array = ['b', 'c', 'd', 'e', 'aa', 'a'],
    group = [],
    result = array
        .map(v => {
            if (v === 'a') {      // first item
                group.unshift(v);
                return group;
            }
            if (v === 'aa') {     // connected item
                group.push(v);
                return [];
            }
            return v;
        })
        .flat();

console.log(...result);

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!