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

339
Views
How to reorder an array to have a key in ascending order, based on an array of values?

I'm trying to run a database migration, and I've run into a chronological issue. I can't change the behaviour, so I have to deal with it somehow.

Suppose I have the following array,

const arr = [
  { model: 'modelE', associations: ['modelA'], order: 1 },
  { model: 'modelA', associations: [ 'modelB' ], order: 2 },
  { model: 'modelB', associations: [], order: 3 },
  { model: 'modelC', associations: ['modelA', 'modelB'], order: 4 },
  { model: 'modelD', associations: ['modelA'], order: 5 },
  { model: 'modelF', associations: [], order: 6 },
]

As we know, we have to create these tables in order, otherwise the foreign keys cannot be created and, therefore, an error will be thrown.

In this case, we have modelE, modelC and modelD that depend on modelA, but modelA depends on modelB, so modelB must be the first element in this array, because it has to be the first table to be created.

The resulting array should look like this:

const arr = [
  { model: 'modelB', associations: [], order: 1 },
  { model: 'modelF', associations: [], order: 2 },
  { model: 'modelA', associations: [ 'modelB' ], order: 3 },
  { model: 'modelC', associations: ['modelA', 'modelB'], order: 4 },
  { model: 'modelE', associations: ['modelC'], order: 5 },
  { model: 'modelD', associations: ['modelA'], order: 6 },
]

Is there an effective way of doing this? All I can think of is running a sort function with arr.finds inside it. Not sure if this is too performant or even readable.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

const sortArrayByDependency = (arr) => {
  const sorted = [];
  const foundSet = new Set();
  const maxLoop = arr.length;
  let loopCount = 0;
  
  while(arr.length > 0 && loopCount < maxLoop) {
    loopCount += 1;
    const first = arr.shift();
    if(first.associations.every(ele => foundSet.has(ele))){
      sorted.push({...first, order: sorted.length + 1 });
      foundSet.add(first.model)
    }
    else {
      arr.push(first)
    }
  }
  
  return sorted;
}

const arr = [
  { model: 'modelB', associations: [], order: 1 },
  { model: 'modelF', associations: [], order: 2 },
  { model: 'modelA', associations: [ 'modelB' ], order: 3 },
  { model: 'modelC', associations: ['modelA', 'modelB'], order: 4 },
  { model: 'modelE', associations: ['modelC'], order: 5 },
  { model: 'modelD', associations: ['modelA'], order: 6 },
]

console.log(sortArrayByDependency(arr))

about 4 years ago · Juan Pablo Isaza Report

0

You are looking for a topological ordering.

You can use a depth-first algorithm for that:

function topoSort(arr) {
    const visited = new Set;
    const map = new Map(arr.map(({model, associations}) => [model, associations]));
    
    function dfs(models) {
        for (let model of models) {
            if (visited.has(model)) continue;
            dfs(map.get(model));
            visited.add(model);
        }
    }
    
    dfs([...map.keys()]);
    return Array.from(visited, (model, i) => 
        ({model, associations: map.get(model), order: i+1})
    );
}

const arr = [
  { model: 'modelE', associations: ['modelA'], order: 1 },
  { model: 'modelA', associations: [ 'modelB' ], order: 2 },
  { model: 'modelB', associations: [], order: 3 },
  { model: 'modelC', associations: ['modelA', 'modelB'], order: 4 },
  { model: 'modelD', associations: ['modelA'], order: 5 },
  { model: 'modelF', associations: [], order: 6 },
]
const result = topoSort(arr);
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!