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

203
Visualizações
Why is this JavaScript working in Firefox but not other browsers?

I have a simple script that works (or should) on page load and sorts a dynamically created list by the list items' Ids. It works fine in Firefox on both Windows and iPad Firefox but not on other browsers, including Chrome, Edge, my Android phone's browser or Amazon's Silk. I thought it might be something to do with my website so I put the code in Codepen and the issue is exactly the same there, too.

When I inspect the code in Chrome and Edge no errors are reported, so the code is just being ignored. Why?

Here is a slightly simplified version:

window.onload = function() {
  let
    list = document.querySelector('.list'),
    results = document.querySelectorAll('.sort');
  let sliced = Array.prototype.slice.call(results, 0);
  sliced.sort(sortByElementId());
  sliced.reduce(function(list, item) {
      list.appendChild(item);
      return list;
    },
    list);

  function sortByElementId() {
    return function(a, b) {
      b.id;
      a.id;
      return a.id > b.id;
    }
  }
}
<div class="day-or-evening">
  <h4>Meeting</h4>
  <ul id="meetings" class="list">
    <li id="id-22" class="sort li-22">Tuesday afternoon</li>
    <li id="id-23" class="sort li-23">Tuesday evening</li>
    <li id="id-25" class="sort li-25">Wednesday evening</li>
    <li id="id-24" class="sort li-24">Wednesday morning</li>
    <li id="id-27" class="sort li-27">Thursday evening</li>
    <li id="id-17" class="sort li-17">Monday afternoon</li>
    <li id="id-26" class="sort li-26">Thursday afternoon</li>
    <li id="id-21" class="sort li-21">Monday evening</li>
  </ul>
</div>

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Your sort comparator function is invalid. A sort comparator must return (for arguments a and b) one of:

  • a negative number, if a should go before b in the result;
  • a positive number, if b should go before a;
  • zero, if a and b are already in order.

If you don't do that, you can get inconsistent and incorrect sorting results. There is no guarantee that for any two elements in your array, for example, that they'll always be passed as a and b (instead of b and a).

For sorting arrays of numbers, subtracting one from the other results in a good return value. For strings, as in your case, you can use the .localeCompare() function of strings, though that has some possibly odd behavior in some special cases.

about 4 years ago · Santiago Trujillo 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