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

81
Vistas
javascript sort list of objects by string containing letters and numbers

I am trying to sort a list of objects by a string which is the filename for a song, sometimes the filename includes numbers and letters, but every sorting method I've tried does not sort the filenames in an order you you would expect, such as in my example below, the expected output is:

Track 1
Track 2
Track 10

but it gets sorted incorrectly as:

Track 1
Track 10
Track 2

var inputFiles=[
  {
    'name':'Track 1.wav'
  },
  {
    'name':'Track 2.wav'
  },
  {
    'name':'Track 10.wav'
  },
]


      console.log('sort audio files by title: ', inputFiles)
      let sortedAudio = inputFiles.sort(function(a, b) {
        var textA = a['name'].toUpperCase(); 
        var textB = b['name'].toUpperCase();
        return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
      });
      console.log('sortedAudio=',sortedAudio)

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

0

Comparing strings with textA < textB will give you results according to the lexiographic order of each character - in which case 1 10 2 is the correct ordering. Compare the differences of the numbers instead of doing a string comparison, for which you'll have to extract only the numeric portion of the string. - can be used, which will convert each side to a number first.

var inputFiles = [{
    'name': 'Track 1.wav'
  },
  {
    'name': 'Track 2.wav'
  },
  {
    'name': 'Track 10.wav'
  },
]


console.log('sort audio files by title: ', inputFiles);
inputFiles.sort((a, b) => a.name.match(/\d+/)[0] - b.name.match(/\d+/)[0]);
console.log('sortedAudio=', inputFiles);

about 4 years ago · Juan Pablo Isaza Denunciar

0

String#localeCompare can handle numbers for you if you set the numeric option. Remember to check if the current browser supports options and fall back to an approach like CertainPerformance's answer if it does not.

var inputFiles = [
  { 'name': 'Track 10.wav' },
  { 'name': 'Track 1.wav' },
  { 'name': 'Track 2.wav' },
  { 'name': 'Track 7.wav' },
  { 'name': 'Unknown Track.wav' },
  { 'name': 'Track 4.wav' },
  { 'name': 'Track 100.wav' },
  { 'name': 'Track 13.wav' },
  { 'name': 'Track 20.wav' },
  { 'name': 'Unknown Track.wav' }
];

const sortedAudio = inputFiles.sort((a,b) => 
  a.name.localeCompare(b.name, undefined, { numeric: true }));

console.log('sortedAudio: ', sortedAudio);

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