Search code examples
angularsortingfilterprimengprimeng-turbotable

Custom sort not working after filtering in PrimeNG table


Custom sorting and filtering work fine individually, but after applying a local filter, the custom sorting doesn't work. On printing the sorting output on the console it gives the expected output, but the same isn't being updated on the frontend.

Here is the stackblitz implementation to the issue: https://stackblitz.com/edit/github-ot8vny?file=src/app/app.component.ts


Solution

  • Import SortEvent

    import { SortEvent } from 'primeng/api';
    

    Then change the sort function to this:

    onSorting(event: SortEvent) {
        event.data.sort((d1, d2) => {
          let v1 = d1[event.field];
          let v2 = d2[event.field];
          return event.order === -1 ? v1.localeCompare(v2) : v2.localeCompare(v1);
        });
      }
    

    You need to use event.data to sort.