Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
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 answers
Answer question

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 Report

0

You can use

isNaN() 

method to check if something is a number or not.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!