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

137
Visualizações
Javascript compare function behaving differently between Firefox and Chrome

I'm sure I'm just missing some simple logic here and it is driving me nuts.

I'm working in Chrome v95.0.4638.69 and Firefox v93.0

I have an array of objects where the data looks like this: [ {id:1, name:"Jake", active:true}, {id:2, name:"Sam", active:false} ]

and my compare function looks like this:

let compare = (a, b) => {
    const Aname = a.name.toUpperCase();
    const Bname = b.name.toUpperCase();
    if (a.active && b.active) {
        return Aname > Bname ? 1 : Aname < Bname ? -1 : 0;
    }
    else {
        return a.active ? 1 : -1;
    }
};

What I'm trying to do is all objects with active:true should be first with name sorted alphabetically followed by objects with active:false sorted alphabetically. In Chrome it does exactly that. In Firefox objects with active = false are at the top of the list. What dumb mistake am I making?

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

0

  1. You should sort by name when both objects have active set to the same value.
  2. When sorting by the active property, -1 should be returned if the first object has the property set to true to order it before the object with the property set to false.
  3. You don't return 0 even if the two objects should be considered equal in the else branch.
let compare = (a, b) => {
    const Aname = a.name.toUpperCase();
    const Bname = b.name.toUpperCase();
    if (a.active === b.active) {
        return Aname > Bname ? 1 : Aname < Bname ? -1 : 0;
    }
    else {
        return a.active ? -1 : b.active ? 1: 0;
    }
};
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