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

202
Visualizações
How to sort, ignoring any characters that are not numbers

I have a sort function that orders items with the class '.sortbox', finds the '.num' value and sorts using flex order. This works fine, but some of the values have extra characters (./-) and this breaks it.

How can I search but ignore all characters that are not numbers?

My attempt here: If I have it all completely wrong then appreciate help to understand why.

https://jsfiddle.net/9cwyund6/1/

function sorting(){
  var items = document.querySelectorAll('.sortbox')
  Array.from(items).sort(function(a, b) {
    a = ~~a.querySelector('.num').innerText
    b = ~~b.querySelector('.num').innerText
    return a - b
  }).forEach(function(n, i) {
    n.style.order = i
  })
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could strip the strings a and b from all non digit characters while sorting

return a.replace(/\D/g,'') - b.replace(/\D/g,'')
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use

isNaN() 

method to check if something is a number or not.

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