说明比较结果的数字。如果 stringObject 小于 target,则 localeCompare() 返回小于 0 的数。如果 stringObject 大于 target,则该方法返回大于 0 的数。如果两个字符串相等,或根据本地排序规则没有区别,该方法返回 0。
function sortTable(a, b) {
// DESC 降序
var state = 0;
if (typeof b[sortKey] == "string" && b[sortKey].localeCompare) {
// 数字 > 中文 > 字母
state = b[sortKey].localeCompare(a[sortKey]);
} else if (typeof b[sortKey] == "number") {
state = b[sortKey] - a[sortKey];
} else {
state = 0;
}
return state;
}
function sortTableUp(a, b) {
var state = 0;
if (typeof a[sortKey] == "string" && a[sortKey].localeCompare) {
state = a[sortKey].localeCompare(b[sortKey]);
} else if (typeof b[sortKey] == "number") {
state = a[sortKey] - b[sortKey];
} else {
state = 0;
}
return state;
}
// 排序逻辑
if (sortKey.length > 0) {
if (sortState == "DESC") {
tableData.sort(sortTable);
} else {
tableData.sort(sortTableUp);
}
}