How to use localeCompare when sorting strings in Chrome browser?

For example, the chineseName.sort((function(a,b){return a.localeCompare(b)}));

javascript localeCompare function works well for IE and firefox, but not in Chrome. Dose, does anyone know how to make the code compatible for Chrome?

+3


source to share


2 answers


I just searched the whole www to find the answer, but alas, nothing ... So I made my own code. This is for Danish letters, so it probably isn't worth it for you, but maybe someone else comes along looking for an answer. This is my first answer to the question, so be careful ...

convert function: The convert function - convert 'double-a' (Danish) [aa] to Danish letter [å]. This will make it easier to compare symbol by symbol later.

DanishCompareCharacter function: This function is the actual comparison function. This refers to the specific Danish characters "æ", "ø" and "å" that are in the END of the Danish alphabet.



DanishCompareString function: what is the custom sort function used as a parameter in the javascript sort function.

function convert(stringToConvert)
{
    var returnString = new String();
    var tempLetter;
    for (var j = 0; j < stringToConvert.length; j++)
        {
            if (stringToConvert.toLowerCase()[j] == 'a' &&         stringToConvert.toLowerCase()[j + 1] == 'a')
                {
                    tempLetter = 'å';
                    j++;
                } else
                {
                    tempLetter = stringToConvert.toLowerCase()[j];

                }
            returnString = returnString + tempLetter;
        }
    return returnString;
}

function danishCompareCharacter(firstCharacter, secondCharacter) {
    if ((firstCharacter == 'æ') || (firstCharacter == 'ø') || (firstCharacter == 'å')) {
            if (secondCharacter == 'æ' || secondCharacter == 'ø' || secondCharacter == 'å') {
                    if (firstCharacter == secondCharacter) {
                            return 0;
                        } 
                        if ((firstCharacter == 'æ') || secondCharacter == 'å')
                                {
                                    return -1;
                                }
                            if ((firstCharacter == 'å') || secondCharacter == 'æ') {
                                    return 1;
                                }
                            }
                return 1;
            } 
    if (secondCharacter == 'æ' || secondCharacter == 'ø' || secondCharacter == 'å') {
            return -1;
        }
        if(firstCharacter==secondCharacter){
                return 0;}
    if( firstCharacter < secondCharacter)
        {
            return -1;
        } 
        return 1;
}

function danishCompareString(a, b)
{
    var result = 0;
    var length = b.length;
    if (a.length < b.length)
        {
            length = a.length;
        }
    for (var i = 0; i < length; i++) {
            result = danishCompareCharacter(a[i], b[i]);
            if (result != 0)
                {
                    return result;
                } 
            }
    return result;
}

function SortMyArray(arr) {
//This works in ALL browsers
arr.sort(function (a, b) {
    return (danishCompareString(convert(a[0]), convert(b[0])));

    //And this would have worked in any browser BUT chrome...
    //        if (a[0].localeCompare(b[0]) == 0) { return 0; }
    //        if (a[0].localeCompare(b[0]) < 0) { return -1; }
    //        else { return 1; }
});
}
}

      

0


source


Works fine in Chrome now (5 years later).

Since you mentioned Chinese names, the function to sort the array of Chinese names would be:

chineseNames.sort(function (a, b) {
  return a.localeCompare(b, 'zh-CN');
});

      



In ES2015, this would be:

chineseNames.sort((a, b) => a.localeCompare(b, 'zh-CN'));

      

See http://www.i18nguy.com/unicode/language-identifiers.html for other language identifiers.

0


source







All Articles