My API retrieves a list of records for me. I would like to display these records sorted by date, with the latest record appearing at the top. However, the TypeScript code I have written does not seem to be ordering my rows correctly. Can anyone assist me in identifying what I may be doing wrong?
this.recommendationService
.getJobExecutionStatList(project.id)
.subscribe(data => {
let data1: any = data;
this.jobExecutionList = new MatTableDataSource();
var sortedArray: Array<any> = data1.sort((n1, n2) => {
let date1 = new Date(n1.executionDate.replace('T','').replace(/-/g,'/'));
let date2 = new Date(n2.executionDate.replace('T','').replace(/-/g,'/'));
if (date1 > date2) {
return 1;
}
if (date1 < date2) {
return -1;
}
return 0;
});
console.log(sortedArray);
this.jobExecutionList.data = sortedArray;
this.jobExecutionList.sort = this.sort;
this.jobExecutionList.paginator = this.paginator;
});
}