1 回答
TA贡献1828条经验 获得超13个赞
let arr = ["", "A001", "V002", "V003", "_123", "133", "2334", "大124", "小afaf", "a001", "v004", "马龙", "中华", "中国"];
arr.sort(function(a, b) {
let max_length = Math.max(a.length, b.length),
compare_result = 0,
i = 0;
while(compare_result === 0 && i < max_length) {
compare_result = compare_char(a.charAt(i), b.charAt(i));
i++;
}
return compare_result;
});
function compare_char(a, b) {
var a_type = get_char_type(a),
b_type = get_char_type(b);
if(a_type === b_type && a_type < 4) {
return a.charCodeAt(0) - b.charCodeAt(0);
} else if(a_type === b_type && a_type === 4) {
return a.localeCompare(b);
} else {
return a_type - b_type;
}
}
function get_char_type(a) {
var return_code = {
nul: 0,
symb: 1,
number: 2,
upper: 3,
lower: 4,
other: 5
}
if(a === '') {
return return_code.nul; //空
} else if(a.charCodeAt(0) > 127) {
return return_code.other;
} else if(a.charCodeAt(0) > 122) {
return return_code.symb;
} else if(a.charCodeAt(0) > 96) {
return return_code.lower;
} else if(a.charCodeAt(0) > 90) {
return return_code.symb;
} else if(a.charCodeAt(0) > 64) {
return return_code.upper;
} else if(a.charCodeAt(0) > 58) {
return return_code.symb;
} else if(a.charCodeAt(0) > 47) {
return return_code.number;
} else {
return return_code.symb;
}
}
console.log(arr);
写的乱了点凑活看吧
添加回答
举报
