MUI X Data Grid sorts in the wrong order or ignores a custom comparator
Column sorts but rows with empty cells jump to the topShort answer
When Data Grid sorting looks wrong, the comparator is usually getting undefined values. After v7 valueGetter takes (value, row) instead of a params object, and a field that does not match a row key gives undefined too. A direction-aware comparator also needs getSortComparator, because the grid reverses a normal one for descending.
Fix it fast
1Find out which cause you have
Log what the comparator really receives for one column. If v1 and v2 print undefined, the problem is in the value, not in the sort.
{ field: 'price', sortComparator: (v1, v2) => { console.log(v1, v2); return v1 - v2; }, }2Match what you see, then jump to its fix
- 1
valueGetter returns undefined for every row after upgrading the grid, and the column shows blank or wrong valuesvalueGetter still uses the old params objectFix - 2
Rows with a missing value are grouped at one end of the list whatever the directionThe column field does not match a key in the row objectsFix - 3
The comparator is correct for ascending, but descending shows an odd order, for example nulls at the wrong endA custom comparator is not symmetric and the grid reverses itFix
- 1
What does the error look like?
Column sorts but rows with empty cells jump to the topsortComparator receives undefined for v1 and v2Descending order is not the opposite of ascending for a custom comparator
Cause 1 of 3
valueGetter still uses the old params object
1Check
The migration guide says valueGetter changed for performance reasons. It no longer receives { value, row }. It receives the value first, then the row, the column and apiRef.
grep -rn "valueGetter" src/2Fix
Rewrite the getter with positional arguments.
// before (v6)
valueGetter: ({ row }) => `${row.firstName} ${row.lastName}`,
// after (v7 and later)
valueGetter: (value, row) => `${row.firstName} ${row.lastName}`,3Verify
The column shows the combined value and the comparator log prints real values.
// reload and click the column header twiceConfirmed by mui.com
Cause 2 of 3
The column field does not match a key in the row objects
1Check
The built-in comparators place null and undefined before every other value. A field name with a typo or different case reads undefined from every row, so there is nothing to order.
console.log(Object.keys(rows[0]));
console.log(columns.map((c) => c.field));2Fix
Make field equal to the property name in the row data, or compute the value with valueGetter.
// rows: [{ id: 1, unitPrice: 9 }]
{ field: 'unitPrice', type: 'number' }3Verify
Click the header once for ascending and again for descending and check both ends of the list.
// click the column header twiceConfirmed by mui.com
Cause 3 of 3
A custom comparator is not symmetric and the grid reverses it
1Check
The grid treats sortComparator as symmetric and simply reverses it for descending order. If your rule depends on the direction, one function cannot express it.
grep -rn "sortComparator" src/2Fix
Use getSortComparator, which receives the sort direction and returns the comparator.
{
field: 'rating',
getSortComparator: (sortDirection) => {
const modifier = sortDirection === 'desc' ? -1 : 1;
return (v1, v2) => modifier * (v1 - v2);
},
}3Verify
Sort ascending and descending and compare the first and last rows.
// click the column header twiceConfirmed by mui.com
How do I stop it coming back?
- Read the grid migration guide before you bump a major version; the value API changed more than once.
- Keep the row property names and column fields identical, and type each column (number, date) so the built-in comparator fits.
- Use the stock comparators for string, number and date columns and write your own only for a real rule.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
The MUI X Data Grid ignores or rejects getRowId
Selection or updates affect the wrong rows
The Data Grid reads each row's id from a property called id by default.
3 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fix