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

183
Vistas
How to sort an alphanumeric array just based on number in javascript?

consider the following array:

['state1035', 'dm5', 'state123', 'county247', 'county2']

sorting this array on basis of number output should be:

['county2' ,'dm5', 'state123', 'county247', 'state1035']
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Assuming the strings will have numbers together and at the end you can use match to extract the number and sort it:

let arr = ['state1035', 'dm5', 'state123', 'county247', 'county2'];
console.log(arr.sort((a,b) => a.match(/\d+$/).pop() - b.match(/\d+$/).pop()));

The match method used within the compare function will return the array of matched sub-strings from the given string. The regex \d+ will match with numbers at the end and we are sorting based on these numbers.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use a regular expression to match against only the numbers in the string, and then sort the strings based on those numbers.

const data = ['state1035', 'dm5', 'bob0Bob', 'state123', 'county247', 'county2'];

const regex = /\d+/;

const result = data.sort((a, b) => {
  return a.match(regex) - b.match(regex);
});

console.log(result);

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