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] Add sort function to MatTableDataSource

See original GitHub issue

Proposal:

Currently inside MatTableDataSource (that is inside master branch, but not released yet) there is sortingDataAccessor function that allows customization of sorting behavior. I propose to enhanсe or replace it with sortFunction

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

For example, I have array of files, some of them are folders. I want to display all folders first and files last. I can try to solve it by using data accessor that return string ${data.isFolder}${data[sortHeaderId]}. However it doesn’t work when sort direction changed from ascending do descending and the folders will be beneath the files.

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

Current master branch of Anular Material

Is there anything else we should know?

I think using sortFunction = (a: T, b: T, ascending: boolean) over sortingDataAccessor is better because sortFunction gives greater flexibility, while sortingDataAccessor just saves a single line of actual comparison.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
rohitkrishna12commented, Jul 4, 2018

@andrewseguin is there any place where we can find an example on how to override sortData to implement custom data sorting?

4reactions
VladislavLobakhcommented, Jan 16, 2019

@rohitkrishna12 it’s a simple example:

this.dataSource.sortingDataAccessor = (data: any, sortHeaderId: string) => {
  const value: any = data[sortHeaderId]; 
  return typeof value === "string" ? value.toLowerCase() : value; 
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular mat-table sort example: Adding sorting to the material ...
Working with sortingDataAccessor in mat-table data source. The sortingDataAccessor function takes two arguments mat-table record and column name and returns ...
Read more >
How to sort MatTableDataSource programmatically?
Try manually sorting the data of the datasource: ... Might be the shortest sort manually function for Material Table.
Read more >
Sorting an Angular Material table - how to use MatSort and ...
With the MatSort directive, the user can sort the data on different columns by clicking on the column header. We use the MatSort...
Read more >
Mattable With Sorting Custom - StackBlitz
MatTable with sorting. custom. ... import {MatTableDataSource, MatSort, Sort}. from '@angular/material'; ... templateUrl: 'table-sorting-example.html',.
Read more >
Implementing Custom Sorting in Columns in JavaScript
I would like to sort table rows by Release Date in descending order, ... To do that, import the MatSort directive and add...
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