最初思路:数组内容排序 sort
-> 字符串排序 sort((a,b)=>a
(这需求还是很简单的么 )(然而排序结果并不是想象的那样)
["吃","安","的","不"]
按照需求应该是abcd字母拼音排序
["安","不","吃","的",]
结果是
["的","安","吃","不"]
升级->出现汉字排序
最开始想法 使用字符串比对 : sort((a,b)=>a
// 并不是我们想要的 拼音顺序
["的","安","吃","不","d","b","a","=","-"," "]
解决:localeCompare() (兼容需考虑)
使用str1.localeCompare(str2);sort((a,b)=>a.localeCompare(b))
// 拼音顺序 字符在前 汉字拼音顺序紧随 字母在后
[" ","-","=","安","不","吃","的","a","b","d"]
var x=['-','=',' ','安','a','不','b','吃','d','的'];
var s1=x.concat([]).sort((a,b)=>a<b);
var s2=x.concat([]).sort((a,b)=>a.localeCompare(b));
console.log(s1,s2)
// 并不是我们想要的 拼音顺序
["的","安","吃","不","d","b","a","=","-"," "]
// 拼音顺序 字符在前 汉字拼音顺序紧随 字母在后
[" ","-","=","安","不","吃","的","a","b","d"]