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

353
Visualizações
How to sort strings by hit count?

I have an string array

const arr = ['ACST', 'CST', 'CCST'];;

And i have const word = 'CS'; How to sort array to be

const newArr = ['CST', 'CCST', 'ACST'];

I tried something like this, but it looks bad, and doesn't work correct; Output CCST -> CST ... , should be CST -> CCST

arr.sort((a, b) => {
            let A = 0;
            let B = 0;
            for (let i = 0; i < word.length; i++) {
                if (a.charAt(i) === word.charAt(i)) {
                    A++;
                }
                if (b.charAt(i) === word.charAt(i)) {
                    B++;
                }
            }
            return A + B;
        });
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You almost got it! You just need to return -1 or 1 to tell the sort algorithm which value to pick.

const arr = ['ACST', 'CST', 'CCST'];;

const newArr = ['CST', 'CCST', 'ACST'];

const word = 'CS'

arr.sort((a, b) => {
    let A = 0;
    let B = 0;
    for (let i = 0; i < word.length; i++) {
        if (a.charAt(i) === word.charAt(i)) {
            ++A;
        }
        if (b.charAt(i) === word.charAt(i)) {
            ++B;
        }
    }
    return A > B ? -1 : 1;
});

console.log(arr)

about 4 years ago · Juan Pablo Isaza Relatório

0

If you want to sort by the number of matching characters at corresponding indexes, you should subtract the count for the two strings in the comparator function (i.e. A - B for ascending order and B - A for descending order).

const arr = ['ACST', 'CST', 'CCST'];
const word = 'CS';
arr.sort((a, b) => {
  let A = 0;
  let B = 0;
  for (let i = 0; i < word.length; i++) {
    if (a.charAt(i) === word.charAt(i))
      A++;
    if (b.charAt(i) === word.charAt(i))
      B++;
  }
  return B - A;
});
console.log(arr);

about 4 years ago · Juan Pablo Isaza Relatório

0

If i got the point correctly:

const arr = ['CRST','CSX', 'CKST', 'CST', 'CCST'];

var lastIndex = 0;
console.log(
  arr.reduce((r, i, index) => {
    if (i.match(/^CS/) || i.match(/^CS./)) {
      r.unshift(i);
      lastIndex = lastIndex+1;
    } else if (i.match(/CS./)) {
      r.splice(lastIndex, 0, i);
    } else r.push(i);

    return r;
  }, [])
);

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