排序将数据转成字符串,逐个字符进行比较
<template>
<el-table
:data="showTable"
@sort-change='sort_change'>
<el-table-column label="更新人员" prop="update_author" sortable='custom'>
</el-table>
</template>
sort_change(column) {
this.currpage = 1;
this.proptype = column.prop;
if (column.order === "descending") {
this.order = 1
this.tableData.sort(this.sortDevName);
} else if (column.order === "ascending") {
this.order = -1
this.tableData.sort(this.sortDevName);
}else{
this.tableData = JSON.parse(JSON.stringify(this.allTable))
}
tableSort(column,this.tableData,this.allTable)
},
sortDevName(str1, str2,a) {
let res = 0
str1[this.proptype] = String(str1[this.proptype])
str2[this.proptype] = String(str2[this.proptype])
if(str1[this.proptype] !== '' && str2[this.proptype] === ''){
return -1
}else if(str2[this.proptype] !== '' && str1[this.proptype] === ''){
return 1
}else{
for (let i = 0; ;i++) {
if (!str1[this.proptype][i] || !str2[this.proptype][i]) {
res = str1[this.proptype].length - str2[this.proptype].length
break
}
const char1 = str1[this.proptype][i]
const char1Type = this.getChartType(char1)
const char2 = str2[this.proptype][i]
const char2Type = this.getChartType(char2)
if (char1Type[0] === char2Type[0]) {
if (char1 === char2) {
continue
} else {
if (char1Type[0] === 'zh') {
res = char1.localeCompare(char2)
} else if (char1Type[0] === 'en') {
res = char1.charCodeAt(0) - char2.charCodeAt(0)
} else {
res = char1 - char2
}
break
}
} else {
res = char1Type[1] - char2Type[1]
break
}
}
}
res = this.order * res
return res
},
getChartType(char) {
if (/^[\u4e00-\u9fa5]$/.test(char)) {
return ['zh', 300]
}
if (/^[a-zA-Z]$/.test(char)) {
return ['en', 200]
}
if (/^[0-9]$/.test(char)) {
return ['number', 100]
}
return ['others', 999]
}