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:

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>
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.
sortComparator: (v1, v2) => parseInt(v1) - parseInt(v2),
work for me
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With