简体   繁体   中英

How to sort a boolean column in antd table?

In my antd table, one of the column is boolean. I want to sort that column. How to do that?

sorter: (a, b) => a.isPayment.localeCompare(b.isPayment),
render: (val)=><div className="text_overlap">{val ? 'Yes':'No'}</div>

I guess localeCompare works only for string.

In JavaScript we have:

true - false === 1
false - true === -1

So all you have to do is just subtracting booleans in your sorter function.

sorter: (a, b) => a - b

In my case, a error message "The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type." was shown in my VSCode.

Here is my solution.

sorter: (a, b) => Number(a.isPayment) - Number(b.isPayment),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM