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

158
Visualizações
How to arrange an array with objects based on another array?

I have this unsorted array (containing objects)

   toSortArray = [{taskID: 1, "title": "something1", subtasks: {}},
                  {taskID: 5, "title": "something5", subtasks: {}},
                  {taskID: 8, "title": "something8", subtasks: {}}];

and I have this array that is dynamically populated based on the correct positioning of the taskIDs

sortingArray = [8, 1, 5];

What I am trying to do is to sort 'toSortArray' with the exact same order as stated in 'sortingArray'.

I have found a somewhat called solution here stating this:

var arr = ['one','four','two'];
var test = [{
    key: 'one'
},{
  key: 'two'
},{
  key: 'four'
}];

function sortFunction(a,b){
    var indexA = arr.indexOf(a['key']);
    var indexB = arr.indexOf(b['key']);
    if(indexA < indexB) {
        return -1;
    }else if(indexA > indexB) {
        return 1;
    }else{
        return 0;        
    }
}

However, it doesn't work for me. I replaced the placeholders for:

function sortFunction (a,b) {
        var indexA = sortingArray.indexOf(a["taskID"]);
        console.log(indexA);
        var indexB = sortingArray.indexOf(b["taskID"]);
        console.log(indexB);
        if (indexA < indexB) {
            return -1;
        } else if (indexA > indexB) {
            return 1;
        } else {
            return 0;
        }
    }

When I debugged (as you can see I am console logging the indexA and indexB). They always return -1, which means no such index is found.

My question is how to get to compare "taskID" value and the sortingArray elements.

Thank you :)

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Disclaimer: In the examples, I've remove properties not relevant to the question, and also renamed taskID to tID for brevity.

You can use Array.prototype.reduce to achieve the desired result:

let arr = [{tID: 1},{tID: 5},{tID: 8}];
let sArr = [8, 1, 5];

const result = sArr.reduce((a,v) => a.concat(arr.find(({tID})=>tID===v)), []);

console.log(result);

You can also use Array.prototype.sort:

let arr = [{tID: 1},{tID: 5},{tID: 8}];
let sArr = [8, 1, 5];

const result = arr.sort(
  ({tID: aID}, {tID: bID}) => sArr.indexOf(aID) - sArr.indexOf(bID));

console.log(result);

Array.prototype.map will also do the job:

let arr = [{tID: 1},{tID: 5},{tID: 8}];
let sArr = [8, 1, 5];

const result = sArr.map(i => arr.find(({tID}) => tID === i));

console.log(result);

map and reduce will probably perform faster than sort because each iteration only requires a single lookup (find) in the toSortArray, as opposed to sort, which requires two lookups (indexOf).

about 4 years ago · Juan Pablo Isaza Relatório

0

You can filter the toSortArray based upon each of the sortingArray element values. Using filter will return an array of one element that matches the current value. Since filter returns an array, use flatMap() to iterate over the sortingArray and return a single, sorted array in the order of the sortingArray.

const toSortArray = [{taskID: 1, "title": "something1", subtasks: {}},
                  {taskID: 5, "title": "something5", subtasks: {}},
                  {taskID: 8, "title": "something8", subtasks: {}}];
                  
const sortingArray = [8, 1, 5];

const result = sortingArray.flatMap(so=>toSortArray.filter(ts=>ts.taskID === so));

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use map and find as:

toSortArray = [{ taskID: 1, "title": "something1", subtasks: {} },
    { taskID: 5, "title": "something5", subtasks: {} },
    { taskID: 8, "title": "something8", subtasks: {} }];
    
sortingArray = [8, 1, 5];
    
const result = sortingArray.map((item) => toSortArray.find((element) => element.taskID === item));
    
console.log(result);
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