I want the special string '--'
always at the bottom regardless of the direction ascending or descending.
The sorted results I expect are:
- Ascend:
['1.1', '1.2', '1.3', '--', '--']
- Descend:
['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: "--" }]}
/>
);
}
I tried this solution, but it doesn’t work.
2
Answers
The
sortOrder
value should be used at both places where you compare with"--"
:CodeSandbox: https://codesandbox.io/s/antd-table-sorter-forked-yj8hmv?file=/src/App.tsx
Try this code to get special string ‘–‘ always at the bottom, may be it will helpful for you