Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can checkboxes be sorted (based on true-false status) in react-table?

I have a table I designed on a 'react-table'. I want to sort according to the checkboxes I marked on my table. For example, when I press column name, the true ones should be listed first. I'll be happy if you can help me.

picture of the error (true values ​​should have been first or false values by choice)

like image 830
msertacc Avatar asked Mar 16 '26 18:03

msertacc


2 Answers

The other solution works, but it's not necessary.

I got stuck with this as well, until I realized that one can set sortType: 'basic' (which is one of the built-in sorts of react-table) and it will sort the boolean column properly.

The reason that sorting a boolean column doesn't work out of the box is because the default sorting is set to alphanumeric.

like image 115
wtfzambo Avatar answered Mar 19 '26 13:03

wtfzambo


After some more research, I learned that it can be done with sortType. We can sort the boolean values ​​by adding this code to the relevant column.

sortType: ((a, b, id) => {
          if (a.original[id] > b.original[id]) return -1; 
          if (b.original[id] > a.original[id]) return 1;
        }),
like image 29
msertacc Avatar answered Mar 19 '26 13:03

msertacc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!