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

285
Vistas
How to sort String from alphabet first then number (in javascript). example if the input is "asd5lk43fj6vc" and the output should be "acdfjklsv3456"

I'm working on a simple sorting program where i need to sort an input from a user to remove whitespace, special character and sort the string(from user input) ascendingly. im new to learning js and i found this code online.

let sortNumButton = document.getElementById('sortNumButton');
let sortOutputContainer = document.getElementById('sortOutputContainer');
let inputField = document.getElementById('inputField');

sortNumButton.addEventListener('click', function(){
let paragraph = document.createElement('p')

paragraph.innerText = inputField.value.split('').sort().join('').replace(/[^a-zA-Z0-9-. ]/g, "");
sortOutputContainer.appendChild(paragraph);
inputField.value = "";
})

the program works but the output prints the number first, not alphabet first

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can .replace the result afterwards, matching numeric characters and putting that capture group at the end instead of the beginning.

const inputValue = "asd5lk43fj6vc";
const sorted = [...inputValue.replace(/[^a-zA-Z0-9-. ]/g, '')].sort().join('');
const result = sorted.replace(/^(\d+)(.*)/, '$2$1');
console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could check with isFinite for numbers and sort then the rest by their value.

const
    sort = string => string
        .split('')
        .sort((a, b) => isFinite(a) - isFinite(b) || a > b || -(a < b))
        .join('');

console.log(sort('asd5lk43fj6vc'));

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