Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material-UI DataGrid not sorting numeric column correctly

I am currently working on DataGrid Component from Material-UI. Data can be displayed, but the inbuilt sorting is not working as expected. When I try to sort ASC/DESC it is sorting based on the first digit of the number. Please look into the picture below:

Descending Order

I have tried the same with fake API in CodeSandbox. It is working fine but in my application the sorting is acting different.

Then I have tried to add sortModel (followed Material-UI Sorting docs) still it is acting the same. I haven't been able to find a solution in the docs.

const sortModel = [{ field: "canaryDeviceId", sort: "asc" }];

<div style={{ height: "90%", width: "100%" }}>
  <DataGrid sortModel={sortModel} rows={rows} columns={columns} />
</div>
like image 358
user8188349 Avatar asked Jul 20 '26 09:07

user8188349


2 Answers

I think DataGrid columns by default use string comparator, if you want to sort numeric column, try setting the type to 'number':

{
  field: "deviceID",
  headerName: "Canary DeviceID",
  type: "number",
},

Then I have tried to add sortModel (followed Material-UI Sorting docs) still it is acting the same.

sortModel only describes the sorting state of DataGrid so I doubt it will solve your problem.

like image 196
NearHuscarl Avatar answered Jul 23 '26 01:07

NearHuscarl


sortComparator: (v1, v2) => parseInt(v1) - parseInt(v2),

work for me

like image 23
dt777 Avatar answered Jul 23 '26 01:07

dt777



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!