/**
* @description 数组里对象根据某个值(包含姓名、字母、数字)排序
* @param null
* @returns {array}
*/
export function compare(data) {
let chineseChars = [],
chars = [],
list = [];
data.forEach(item => {
// 判断是否为中文
if (/^[\u4e00-\u9fa5]*$/.test(item.name.charAt(0))) {
chineseChars.push(item); // 姓名首字符为中文的
} else {
chars.push(item); // 姓名首字符非中文的(字母,数字)
}
});
chars.sort((a, b) => a.name.charCodeAt(0) - b.name.charCodeAt(0));
chineseChars.sort((a, b) => a.name.localeCompare(b.name));
list = chars.concat(chineseChars); // list为最终想要的数组
return list
}
数据示例
roles: [
{ name: '上海', id: 1, phone: '18989898998' },
{ name: '广州', id: 2, phone: '18989898998' },
{ name: '北京', id: 3, phone: '18989898998' },
{ name: '深圳', id: 4, phone: '18989898998' },
{ name: '石家庄', id: 5, phone: '18989898998' },
{ name: '唐山', id: 6, phone: '18989898998' },
{ name: '秦皇岛', id: 7, phone: '18989898998' },
{ name: '邯郸', id: 8, phone: '18989898998' },
{ name: '邢台', id: 9, phone: '18989898998' },
{ name: '保定', id: 10, phone: '18989898998' },
{ name: '张家口', id: 11, phone: '18989898998' },
{ name: '承德', id: 12, phone: '18989898998' },
{ name: '沧州', id: 13, phone: '18989898998' },
{ name: '廊坊', id: 14, phone: '18989898998' },
],