2012-04-08 54 views

回答

0

我剛剛搜索整個www找到答案,但唉,什麼都沒有......所以我做了我自己的代碼。這是丹麥的信件,所以它可能不值得你很多,但也許有人來尋找答案。這是我對一個問題的第一個答案,所以要溫柔...

convert function:convertfunction是將'double-a'(丹麥語)[aa]轉換爲丹麥語字母[å]。這將使以後逐字比較字符變得更容易。

danishCompareCharacter功能:該功能是實際的比較功能。它適用於丹麥alfabet結尾的特定丹麥字符'æ','ø'和'å'。

danishCompareString函數:這是自定義排序函數用作JavaScript排序函數中的參數。

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

它現在在Chrome中運行得很好(5年後)。

既然你提到中國人的名字,功能整理中國名稱數組是:

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

在ES2015這將是:

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

其他語言的標識符見http://www.i18nguy.com/unicode/language-identifiers.html

相關問題