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

124
Visualizações
How to set the elements of an array according to a certain rule

Imagine we have array like this:

const words = ['luigi', 'Bar', 'Test', 'zac1', 'Alf0'];

Now I would like to sort this array alphabetically:

words.sort((a, b) => a.localeCompare(b));

Imagine we have array of super words. This words have high priority and should be on the top of 'words list'.

const superWords = ["zac1", "Test"]

So the desired result of words array will be:

const desiredResult = ["Test", "zac1","Alf0", "Bar", "luigi"]

How can I do it?

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

0

You could convert superWords into a Set for O(1) lookup time using has rather than O(n) using includes when superWords is an Array (where n is the number of words in superWords):

const words = ['luigi', 'Bar', 'Test', 'zac1', 'Alf0'];
const superWords = ['zac1', 'Test'];
const superWordsSet = new Set(superWords); // This is O(n) but you only need to do this once.
words.sort((a, b) => {
    if (superWordsSet.has(a) && !superWordsSet.has(b)) {
        return -1;
    } else if (!superWordsSet.has(a) && superWordsSet.has(b)) {
        return 1;
    }
    return a.localeCompare(b);
});
console.log(words);

Performance

The Set has method checks if a value is in a Set object, using an approach that is, on average, quicker than testing most of the elements that have previously been added to the Set object. In particular, it is, on average, faster than the Array.prototype.includes method when an Array object has a length equal to a Set object's size.

about 4 years ago · Juan Pablo Isaza Relatório

0

you can do this

const words = ['luigi', 'Bar', 'Test', 'zac1', 'Alf0'];


words.sort((a, b) => {
 const superWords = ["zac1", "Test"]
 if(superWords.includes(a) && !superWords.includes(b)){
  return -1
 }else if(superWords.includes(b) && !superWords.includes(a)){
  return 1
 }
 return a.localeCompare(b)

})

console.log(words)

about 4 years ago · Juan Pablo Isaza Relatório

0

First step find doubles and the your can sort both arrays and afterwards you can concat the arrays.

let words = ['luigi', 'Bar', 'Test', 'zac1', 'Alf0'];
const superWords = ["zac1", "Test"]

words = words.filter(w => ! superWords.includes(w));

words.sort((a, b) => a.localeCompare(b));
superWords.sort((a, b) => a.localeCompare(b));



console.log(superWords.concat(words));

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