我希望特殊字符串'--'
总是在底部,无论方向是上升还是下降。
我期望的排序结果是:
- 升序:
['1.1', '1.2', '1.3', '--', '--']
- 规格:
['1.3', '1.2', '1.1', '--', '--']
import "antd/dist/antd.css";
import { Table } from "antd";
function sorter(sortOrder) {
return function (a, b) {
// equal items sort equally
if (a === b) {
return 0;
}
// nulls sort after anything else
if (a === "--") {
return 1;
}
if (b === "--") {
return -1;
}
// otherwise, if we're ascending, lowest sorts first
if (sortOrder === "ascend") {
return a < b ? -1 : 1;
}
// if descending, highest sorts first
return a < b ? 1 : -1;
};
}
export default function App() {
return (
<Table
columns={[
{
dataIndex: "value",
title: "value",
sorter: (aRecord, bRecord, sortOrder) => sorter(sortOrder)(aRecord.value, bRecord.value),
},
]}
dataSource={[{ value: "1.1" }, { value: "--" }, { value: "1.3" }, { value: "1.2" }, { value: "--" }]}
/>
);
}
字符串
我试过这个solution,但它不工作。
的数据
codesandbox的
2条答案
按热度按时间cngwdvgl1#
sortOrder
值应在与"--"
进行比较的两个位置使用:字符串
CodeSandbox:https://codesandbox.io/s/antd-table-sorter-forked-yj8hmv?file=/src/App.tsx
ryoqjall2#
试试这段代码来获得特殊的字符串'--'总是在底部,可能会对你有帮助
字符串