Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

201
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

You can use

isNaN() 

method to check if something is a number or not.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda