Mui-datatables: customSort only one column

Created on 21 Mar 2019  路  11Comments  路  Source: gregnb/mui-datatables

I am using customSort because I have a column with dates. However, all of the other columns are strings or integers and simply need to be sorted by default. Is there a way to only apply the customSort to one column?

question

Most helpful comment

I suggest adding customSort option to the columns definition, that would be much better.

All 11 comments

Just checking in to see if their is a solution for this?

I too would like to know if there is a way to do this.

You can use a logic block in the customSort function and key off the column index of the date column. Something like this:
customSort: (data, colIndex, order) => { return data.sort((a, b) => { if (colIndex === 3 || colIndex === 6) { return (new Date(a.data[colIndex]) < new Date(b.data[colIndex]) ? -1: 1 ) * (order === 'desc' ? 1 : -1); } else { return (a.data[colIndex] < b.data[colIndex] ? -1: 1 ) * (order === 'desc' ? 1 : -1); } }); }

I suggest adding customSort option to the columns definition, that would be much better.

rcfish this helped a lot, thank you

@alielkhateeb's solution would be nicer, @rcfish's solution works but required hardcoding/tracking of the column index.

Instead of customSort option to the columns , new version of mui-datatables provides sortCompare option to the columns. I am trying to use it as the documentation (https://www.npmjs.com/package/mui-datatables/v/3.3.1) says it's there but couldn't see that in MUIDataTableColumnOptions, not sure why.

@pawarakesh what do you mean you don鈥檛 see it in MUIDatatableColumnOptions? I鈥檓 not sure what that refers to.

@patorjk - this refers to the type for column option. if you see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/mui-datatables/index.d.ts , I am not able to see sortCompare under MUIDataTableColumnOptions

Ah, I see. I'm not sure who updates that, though I have noticed it's been getting updated. You'll have to put in a PR over there.

thanks @patorjk

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gangakrishh picture gangakrishh  路  3Comments

JordanKadish picture JordanKadish  路  4Comments

chapmanjacobd picture chapmanjacobd  路  4Comments

Aankhen picture Aankhen  路  3Comments

krsandesh picture krsandesh  路  3Comments