2
votes

In my ag-grid Angular application, I am trying to sort data as per date-time. I am converting date into EST(am, pm format). I want the data to be sorted in descending order when the page loads the first time and the sorting icon on the column should work properly (ascending/ descending order)! But the issue I am running into is that sorting is happening fine as per date but not as per time. For eg. 2019-Jan-23, 2:38:41 pm should come above 2019-Jan-23, 6:38:41 am and 2019-Jan-23, 9:38:41 am

Below is my working Stackblitz:

https://stackblitz.com/edit/ag-grid-angular-hello-world-32fnmi?file=src/app/app.component.ts

1

1 Answers

0
votes

You will have to use a custom date comparator:

{field: "date", headerName: "DATE", headerTooltip: "DATE",  width: 150, minWidth: 100, maxWidth: 150, resizable: true,
        valueFormatter: params => this.helper.dateFormater(params.value),
        comparator: this.helper.dateComparator
      },

Date comparator could be something like:

 dateComparator(date1: string, date2: string): number {
    return new Date(date1) < new Date(date2) ? -1 : 1;
  }