Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

MUI X Data Grid sorts in the wrong order or ignores a custom comparator

ErrorColumn sorts but rows with empty cells jump to the top

Short 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.

  • Applies to MUI X Data Grid v7, MUI X Data Grid v8, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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;
      },
    }
  2. 2Match what you see, then jump to its fix

    1. 1valueGetter returns undefined for every row after upgrading the grid, and the column shows blank or wrong valuesvalueGetter still uses the old params objectFix
    2. 2Rows 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. 3The 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

What does the error look like?

  • Column sorts but rows with empty cells jump to the top
  • sortComparator receives undefined for v1 and v2
  • Descending 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 twice

Confirmed by mui.com

Did this fix it?

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 twice

Confirmed by mui.com

Did this fix it?

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 twice

Confirmed by mui.com

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.