1.基本数字排序
官网写的
sorter: { compare: (a, b) => a.chinese - b.chinese }
2.汉字排序
{
title: '名称',
dataIndex: 'name',
key: 'name',
sorter: (a, b) => a.name.localeCompare(b.name), // 启用排序,这里是按名称的字母顺序排序
},
3.时间排序
{ title: '发生时间', dataIndex: 'time', key: 'time', ellipsis: true, align: 'center', sorter: { compare: (a, b) => Date.parse(a.time) - Date.parse(b.time), }, },
4.数字和字母组合排序
{
title: siteId,
dataIndex: 'abcId',
key: 'abcId',
sorter: (a, b) => {
// 自定义排序函数
const aVal = a.abcId.toLowerCase();
const bVal = b.abcId.toLowerCase();
let aNum = '', bNum = '';
// 提取数字部分
const aNumMatch = aVal.match(/\d+/);
if (aNumMatch) {
aNum = aNumMatch[0];
}
const bNumMatch = bVal.match(/\d+/);
if (bNumMatch) {
bNum = bNumMatch[0];
}
// 先比较字母部分
if (aVal.replace(/\d+/, '') !== bVal.replace(/\d+/, '')) {
return aVal.localeCompare(bVal);
}
// 如果字母部分相同,再比较数字部分
if (aNum !== bNum) {
return parseInt(aNum, 10) - parseInt(bNum, 10);
}
// 如果都相同,则认为它们相等
return 0;
},
//sortDirections: ['descend'],
},