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

73
Visualizações
Sort two array at the same time based on each other

I have two arrays sorted and ready like this:

const mains = [1, 5, 6, 8 , 12];
const secondaries = [1, 6, 12];

I want to create a div with multiple spans dynamically based on above arrays like theses:

<span class="main">${unit}</span>

<span class="secondary">${unit}</span>

The issue is I'm unable to find a proper solution to sort both arrays and create those spans .

In the given arrays the result should be this:

<span class="main"> 1 </span>

<span class="secondary"> 1 </span>

<span class="main"> 5 </span>

<span class="main"> 6 </span>

<span class="secondary"> 6 </span>

<span class="main"> 8 </span>

<span class="main"> 12 </span>

<span class="secondary"> 12 </span>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Loop over the mains array, and check if there's a corresponding element in secondaries:

html = '';
mains.foreach(main => {
    html += `<span class="main"> {$main} </span>`;
    if (secondaries.includes(main) {
        html += `<span class="secondary"> {$main} </span>`;
    }
});

If the arrays are large, you should convert secondaries to a Set and use secondaries.has(main) instead of secondaries.includes(main).

about 4 years ago · Juan Pablo Isaza Relatório

0

const mains = [1, 5, 6, 8 , 12];
const secondaries = [1, 6, 12];

const min = Math.min(...mains, ...secondaries);
const max = Math.max(...mains, ...secondaries); 

for (let i=min; i<=max; i++){
  if (mains.includes(i)) console.log(`<span class="main">${i}</span>`);
  if (secondaries.includes(i)) console.log(`<span class="secondaries">${i}</span>`);
}

about 4 years ago · Juan Pablo Isaza Relatório

0

You could merge them together, and as you merge them, create the span:

function span(val, class){
    console.log(`<span class=${class}>${val}</span>`);
function mergeAndSpan(main, secondary){
    let i = 0, j = 0;
    while(i < main.length && j < secondary.length){
        if(main[i] < secondary[j]){
            span(main[i], 'main')
            i++;
        } else {
            span(secondary[j], 'secondary')
            j++;
        }
    }
    while(i < main.length){
        span(main[i], 'main')
        i++;
    }
    while(j < secondary.length){
        span(secondary[j], 'secondary')
        j++;
    }
}
mergeAndSpan(mains, secondaries);

This is effectively the merge portion of mergesort, modified slightly to create spans instead of putting the elements into an array. What it does is iterates through both, creating a span for the lower element, and incrementing its' variable. Then, once one of the arrays is empty, then it iterates through the other array and creates the span for each of its' elements.

And, because it is performing an operation on all of n elements, it takes O(n) time.

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