2019-11-15 14:35:25 +08:00
---
order: 7
title:
en-US: Multiple sorter
zh-CN: 多列排序
---
## zh-CN
`column.sorter` 支持 `multiple` 字段以配置多列排序优先级。通过 `sorter.compare` 配置排序逻辑,你可以通过不设置该函数只启动多列排序的交互形式。
## en-US
`column.sorter` support `multiple` to config the priority of sort columns. Though `sorter.compare` to customize compare function. You can also leave it empty to use the interactive only.
2022-05-19 09:46:26 +08:00
```tsx
2019-11-15 14:35:25 +08:00
import { Table } from 'antd';
2022-07-04 22:09:54 +08:00
import type { ColumnsType, TableProps } from 'antd/es/table';
2022-05-23 14:37:16 +08:00
import React from 'react';
2019-11-15 14:35:25 +08:00
2022-05-19 09:46:26 +08:00
interface DataType {
key: React.Key;
name: string;
chinese: number;
math: number;
english: number;
}
const columns: ColumnsType< DataType > = [
2019-11-15 14:35:25 +08:00
{
title: 'Name',
dataIndex: 'name',
},
{
title: 'Chinese Score',
dataIndex: 'chinese',
sorter: {
compare: (a, b) => a.chinese - b.chinese,
multiple: 3,
},
},
{
title: 'Math Score',
dataIndex: 'math',
sorter: {
compare: (a, b) => a.math - b.math,
multiple: 2,
},
},
{
title: 'English Score',
dataIndex: 'english',
sorter: {
compare: (a, b) => a.english - b.english,
multiple: 1,
},
},
];
2022-05-19 09:46:26 +08:00
const data: DataType[] = [
2019-11-15 14:35:25 +08:00
{
key: '1',
name: 'John Brown',
chinese: 98,
math: 60,
english: 70,
},
{
key: '2',
name: 'Jim Green',
chinese: 98,
math: 66,
english: 89,
},
{
key: '3',
name: 'Joe Black',
chinese: 98,
math: 90,
english: 70,
},
{
key: '4',
name: 'Jim Red',
chinese: 88,
math: 99,
english: 89,
},
];
2022-05-19 09:46:26 +08:00
const onChange: TableProps< DataType > ['onChange'] = (pagination, filters, sorter, extra) => {
2019-11-15 14:35:25 +08:00
console.log('params', pagination, filters, sorter, extra);
2022-05-19 09:46:26 +08:00
};
const App: React.FC = () => < Table columns = {columns} dataSource = {data} onChange = {onChange} / > ;
2019-11-15 14:35:25 +08:00
2022-05-19 09:46:26 +08:00
export default App;
2019-11-15 14:35:25 +08:00
```