假设:一个listData数组结构如图,要想根据数组对象中的name进行类似Excel表头数据排序(A1,A10,B2),用js如何实现?
compare (name) {
return function (s, t) {
let a = s[name].toLowerCase();
let b = t[name].toLowerCase();
if (a.length === 2) {
a = a.slice(0, a.length - 1) + '0' + a.slice(-1)
}
if (b.length === 2) {
b = b.slice(0, b.length - 1) + '0' + b.slice(-1)
}
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
}
listData.sort(this.compare('name'))