/**
* 字符串排序比较,支持中英文混合,数字<英文<中文,数字按照ASCII码;英文按字母顺序,大小写按照ASCII码;中文按照拼英字母顺序;
* @param {string | number} param1 排序字符串1
* @param {string | number} param2 排序字符串2
* @return 1:param1大于param2 ,-1: param1小于param2,0:相等
*/
var compare = function(param1, param2) {
if ("number" === typeof param1 && "number" === typeof param2) {
return param1 > param2 ? 1 : param1 < param2 ? -1 : 0;
}
if ("number" === typeof param1 && "string" === typeof param2) {
return -1;
}
if ("string" === typeof param1 && "number" === typeof param2) {
return 1;
}
if ("string" === typeof param1 && "string" === typeof param2) {
var reg = /[\u4E00-\u9FFF]+/;
var length1 = param1.length;
var length2 = param2.length;
var length = length1 > length2 ? length2 : length1;
var char1, char2;
var temp1, temp2;
var match1, match2;
for (var i = 0; length > i; i++) {
char1 = param1.charAt(i);
char2 = param2.charAt(i);
match1 = reg.test(char1);
match2 = reg.test(char2);
if (match1 && match2) {
if (0 === char1.localeCompare(char2)) {
continue;
}
return char1.localeCompare(char2);
}
if (!match1 && match2) {
return -1;
}
if (!match2 && match1) {
return 1;
}
temp1 = char1.toUpperCase();
temp2 = char2.toUpperCase();
if (temp1 > temp2) {
return 1;
}
if (temp2 > temp1) {
return -1;
}
if (char1 > char2) {
return 1;
}
if (char2 > char1) {
return -1;
}
}
return length1 > length2 ? 1 : length2 > length1 ? -1 : 0;
}
};
console.log(compare("asdf", "家连锁店"));
console