question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Table] MatTableDataSource does not properly sort null/undefined values

See original GitHub issue

Bug, feature request, or proposal:

Sort of bug, sort of feature request

What is the expected behavior?

rows with undefined values for a sorted column should sort properly

What is the current behavior?

null/undefined values cause random sorting issues

What are the steps to reproduce?

Providing a StackBlitz/Plunker (or similar) is the best way to get the team to see your issue.
Plunker starter (using on @master): https://goo.gl/uDmqyY
StackBlitz starter (using latest npm release): https://goo.gl/wwnhMV

Have a sortable mat-table with some null values in some columns. Try to sort.

What is the use-case or motivation for changing an existing behavior?

columns often have empty values

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

material 5.0.0rc0

Is there anything else we should know?

MatTableDataSource has this line where the actual sorting is done:

return (valueA < valueB ? -1 : 1) * (direction == 'asc' ? 1 : -1);

Note that like values are not treated correctly either. This could be replaced with the following:

     return this.compareItems(valueA, valueB) * (direction == 'asc' ? 1 : -1);

    private compareItems(itemA: any, itemB: any): number {
      let retVal: number = 0;
      if (itemA && itemB) {
        if (itemA > itemB) retVal = 1;
        else if (itemA < itemB) retVal = -1;
      }
      else if (itemA) retVal = 1;
      else if (itemB) retVal = -1;
      return retVal;
    }

Also, to make strict null safe, the sortingDataAccessor method should be modified to allow such a value.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:10
  • Comments:10

github_iconTop GitHub Comments

2reactions
gangsthubcommented, Feb 8, 2018

@yedidisatya it has been published with the lastest release so far: https://github.com/angular/material2/releases/tag/5.2.0

Update your library:

# if you are using npm:
npm install --save @angular/material @angular/cdk

# if you are using yarn:
yarn add @angular/material @angular/cdk

And that’s it! 😉

1reaction
V2NileshSukalikarcommented, Mar 28, 2018

@gangsthub, by upgrading the packing this seems to be not working. I still see the same result while sorting the data when the rows have null/empty or undefined values. Kindly suggest.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular material table sorting doesn't work correctly
Angular @ViewChild decorator can't recognize MatSort instance in your template since you're importing MatSort from two different packages:.
Read more >
mdbTable does not sort when property value is NULL.
The mdbTable does not sort correct when having property values that is NULL. When debugging, the following (compare) function will return 0 if...
Read more >
Sort Mattable According To The Selected Value Of Matselect
[Table] MatTableDataSource does not properly sort null/undefined values Have a sortable mattable with some null values in some columns.
Read more >
Table | Angular Material
Accessor function to retrieve the data rendered for each cell. If this property is not set, the data cells will render the value...
Read more >
angular/angular - Gitter
@alxhub the Angular tests? are they not unit tests? Mike Tung ... the subscriber receive the error event, but the error object there...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found