对包含汉字和字母的对象数组进行升序排列
export function ascendSort(arr, propName, empty) {
let chineseChars = [],
chars = [],
list = [];
(arr || []).forEach(item => {
// 判断是否为中文
if (/^[\u4e00-\u9fa5]*$/.test(item[propName].charAt(0))) {
chineseChars.push(item); // 姓名首字符为中文的
} else {
chars.push(item); // 姓名首字符非中文的(字母,数字)
}
});
chars.sort((a, b) => a[propName].charCodeAt(0) - b[propName].charCodeAt(0));
chineseChars.sort((a, b) =>
a[propName].charAt(0).localeCompare(b[propName].charAt(0))
);
list = chars.concat(chineseChars); // list为最终想要的数组
return list;
}
posted on 2022-02-18 15:13 sunshinezjb 阅读(81) 评论(0) 收藏 举报
浙公网安备 33010602011771号