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

128
Vistas
Confusing behaviour of localeCompare with sort

So when I try to sort alphabets with just sort, it works as expected like:

['a', 'A'].sort().join('') // 'Aa'

However, when I use localeCompare with sort, it doesn't work as expected:

['a', 'A'].sort((a, b) => a.localeCompare(b)).join('') // 'aA'    

OR

['a', 'b', 'A', 'c', 'b', 'A', 'B'].sort((a, b) => a.localeCompare(b)).join('')

// 'aAAbbBc'

Why doesn't the sort place uppercase letters like 'A' before lowercase case letters as would have happened when using just sort() with no arguments ? I'm trying to sort the letters in ascending order (lexicographically).Thanks!

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

0

The two are indeed not guaranteed to use the same algorithm for string comparison:

ECMAScript on localeCompare has:

The two Strings are compared in an implementation-defined fashion.

[...]

This function is intended to rely on whatever language-sensitive comparison functionality is available to the ECMAScript environment from the host environment, and to compare according to the rules of the host environment's current locale.

ECMAScript on sort on the other hand has this step in its procedure:

Let xSmaller be ! IsLessThan(xString, yString, true).

isLessThan is defines the comparison in specific terms (referring to code points), and it is the same procedure that is used for determining whether a < b.

This should explain why there can be a difference.

A callback function that is equivalent

If you are looking for a sort callback function that will have the same effect as when you would not have provided one, then go for this one:

let cmp = (a, b) => (String(a) > String(b)) - (String(a) < String(b));

// Demo
let arr = ['a', 'A', undefined, 4, NaN, Infinity, null];
let sorted1 = [...arr].sort(cmp);
let sorted2 = [...arr].sort();

console.log(sorted1);
console.log(sorted2);

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