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

183
Visualizações
How to sort an array based off an array of indexes that are in order?

I'm trying to sort an array of words based off an array of indexes. Here is the array of words:

const animalArray = ['Wolf', 'Cat', 'Dog', 'Rabbit', 'Random']

Here is an Array of indexes that i have been given that correlate to the order the animal array above needs to be in:

const indexArray = [ 3, 0, 1, 2 ] 

So in indexArray, we start off with index 3. This correlates to 'Rabbit' in the animalsArray. 3 is at index 0 in the indexArray so we should should move 'Rabbit' to index 0. We should keep doing this until we reach the end of the array. Any extras should be added to the end of the array. So once the function runs, we should get the following result:

sortArray(animalArray, indexArray); // ['Rabbit', 'Wolf, 'Cat', 'Dog', 'Random']

I have figured out the first part (create the index array). However, i don't know how to get them in the correct order. How can i put the animalsArray in the correct order?

EDIT:

I oversimplified this and ended up not asking the question correctly.

We should actually pass 3 params to the sortArray, the third being an object that gets sorted:

const animalObjects = [
 { 
  name: 'the Wolf goes to the beach'
 },
 {
  name: 'the Cat climbs a tree'
 },
 {
  name: 'the Dog runs around'
 },
 {
  name: 'the Rabbit burrows'
 },
 {
  name: 'Random extra object that should be at the end'
 }
]

It should sort like so:

sortArray(animalArray, indexArray, animalObjects);

/* returns -

[
 {
  name: 'the Rabbit burrows'
 },
 { 
  name: 'the Wolf goes to the beach'
 },

 {
  name: 'the Cat climbs a tree'
 },
 {
  name: 'the Dog runs around'
 },
 {
  name: 'Random extra object that should be at the end'
 }
]

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

0

var sortedAnimalArray =  []

for(var i = 0; i < indexArray.length; i++ {
   sortedAnimalArray[i] = animalArray[indexArray[i]]
} 
about 4 years ago · Juan Pablo Isaza Relatório

0

Map the indexArray to access each element at the index, then go through the animals to identify holes.

const animalArray = ['Wolf', 'Cat', 'Dog', 'Rabbit', 'Random'];
const indexArray = [ 3, 0, 1, 2 ];

const indicies = new Set(indexArray);
const results = indexArray
  .map(index => animalArray[index])
  .concat(
    animalArray.filter((_, index) => !indicies.has(index))
  );
console.log(results);

about 4 years ago · Juan Pablo Isaza Relatório

0

To make it simple.

  • Use Array.map() to iterate the indexArray
  • Compare the elements in both the array using Array.indexOf() and push the remaining element into a result array.

Try this :

const animalObjects = [{ 
  name: 'the Wolf goes to the beach'
 }, {
  name: 'the Cat climbs a tree'
 }, {
  name: 'the Dog runs around'
 }, {
  name: 'the Rabbit burrows'
 }, {
  name: 'Random extra object that should be at the end'
}];
const indexArray = [ 3, 0, 1, 2 ];

const newArray = indexArray.map((item) => animalObjects[item]);

animalObjects.forEach((animal) => {
    if (newArray.indexOf(animal) === -1) {
    newArray.push(animal)
  }
})

console.log(newArray);

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