效果:点击按钮,arr升序、降序和原始数组循环切换。
let arr= [
{ input: 'hello' },
{ input: 'world' },
{ input: 'foo' }
];
arr.sort((a, b) => a.input.localeCompare(b.input));
<Button onClick={() => console.log(arr)} />
可以用在antd的Table组件里。
const columns = [
{
title: '姓名'
dataIndex: 'name',
sorter: (a, b) => a.name.localeCompare(b.name),
},
{
title: '编号',
dataIndex: 'number',
sorter: (a, b) => a.number.localeCompare(b.number),
},
]