Efficiently replace all accented characters in a string?

后端 未结 21 2829
别跟我提以往
别跟我提以往 2020-11-22 04:35

For a poor man\'s implementation of near-collation-correct sorting on the client side I need a JavaScript function that does efficient single character rep

21条回答
  •  孤城傲影
    2020-11-22 05:20

    Not a single answer mentions String.localeCompare, which happens to do exactly what you originally wanted, but not what you're asking for.

    var list = ['a', 'b', 'c', 'o', 'u', 'z', 'ä', 'ö', 'ü'];
    
    list.sort((a, b) => a.localeCompare(b));
    
    console.log(list);
    
    //Outputs ['a', 'ä', 'b', 'c', 'o', 'ö', 'u', 'ü', 'z']
    

    The second and third parameter are not supported by older browsers though. It's an option worth considering nonetheless.

提交回复
热议问题