11

Noticed something potentially odd with JavaScript's sort() method. Given the following array:

var arr = ['Aaa',
'CUSTREF',
'Copy a template',
'Copy of Statementsmm',
'Copy1 of Default Email Template',
'Copy11',
'Cust',
'Statements',
'zzzz'];

Calling sort on this array:

console.log(arr.sort());

Yields:

["Aaa", "CUSTREF", "Copy a template", "Copy of Statementsmm", "Copy1 of Default Email Template", "Copy11", "Cust", "Statements", "zzzz"] 

Is this correct? ie. CUSTREF is listed first, is this because of it's capital letters?

benhowdle89
  • 36,900
  • 69
  • 202
  • 331
  • That's how [`sort()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) works. Characters are sorted by their Unicode values. – Teemu Feb 11 '14 at 11:41
  • possible duplicate of [How to perform case insensitive sorting in Javascript?](http://stackoverflow.com/questions/8996963/how-to-perform-case-insensitive-sorting-in-javascript) – Liam Feb 11 '14 at 11:43

5 Answers5

20

That is correct. The strings are being sorted in a binary fashion, using the ordinal values of the characters themselves.

For a case-insensitive sort, try this:

arr.sort(function(a,b) {
    a = a.toLowerCase();
    b = b.toLowerCase();
    if( a == b) return 0;
    return a < b ? -1 : 1;
});
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
13

You are correct, it is because of capital letters. If you are sorting strings which might have non ASCII characters such as ä and ö you should use String.localeCompare(). This also fixes the capital letter problem.

arr.sort(function (a, b) {
    return a.localeCompare(b);
});
Mika Tuupola
  • 19,877
  • 5
  • 42
  • 49
3

Yes, It has a higher Unicode value. (it = the 'U' in the first word)

you maybetter use

.sort(function(a,b) { return (a.toLowerCase() < b.toLowerCase()) ? -1 : 1;});

Ori Refael
  • 2,888
  • 3
  • 37
  • 68
0

if you look at they way characters are encoded (eg. ASCII table) you will see, that capital letters have lower values that lowercase one's, so yes - it's because of capital letters

pwolaq
  • 6,343
  • 19
  • 45
0

Since U (U+0055) has a lesser Unicode value than o (U+006F), a case-sensitive sort will always place U before o. For a case-insensitive sort, you should try:

arr.sort(
    function(a, b){
        if (a.toLowerCase() < b.toLowerCase()) return -1;
        if (a.toLowerCase() > b.toLowerCase()) return 1;
        return 0;
    }
);
Sharanya Dutta
  • 3,981
  • 2
  • 17
  • 27