如何对包含特殊字符和空格的字符串数组进行排序

how to sort an array of string which contains special characters and white spaces

我想对包含特殊字符和空格的字符串数组进行排序。排序时我想忽略特殊字符,以便数组排序仅基于字符和数字。

例如:数组就像: ["ibtp-17","personal (z)","personal (a)","(z)","yabcd","y(3)"]

只需要智能逻辑来实现它。任何帮助,将不胜感激。谢谢


到目前为止,我已经尝试使用替换,它有时会给我正确的结果,有时却不会。

 ["ibtp-17","personal (z)","personal (a)","(z)","yabcd","y(3)"]
 .sort(function(a,b){
    return a.replace(/[^A-Z0-9]/ig, "") > b.replace(/[^A-Z0-9]/ig, "")
 }) 

因为我不应该只是 post 代码(因为 OP 需要先自己尝试一些东西,还有那些爵士乐)。这是一些逻辑:

Array.sort() 接受比较函数。在上述比较函数中,您可以放置​​一些基于正则表达式的特殊字符剥离器,例如 String.replace(/[^a-zA-Z0-9 ]/g, "");。然后比较结果字符串。干杯。