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

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

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 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!