I am using a Mat List to display a lot of data. The mat list is used to order the data depending on the column values.
I have this working compare function which sorts alphabetic or numerical, depending of the input type:
function compare(a, b, isAsc) {
return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}
The table gets sorted by this function:
this.sortedData = data.sort((a, b) => {
const isAsc = sort.direction === 'asc';
switch (sort.active) {
case 'OrderId':
return compare(+a.OrderId, +b.OrderId, isAsc);
case 'Site':
return compare(a.Site, b.Site, isAsc);
case 'OrderDate':
return compare(a.OrderDate, b.OrderDate, isAsc);
case 'Email':
return compare(a.Email, b.Email, isAsc);
case 'Phone':
return compare(a.Phone, b.Phone, isAsc);
case 'FullName':
return compare(a.FullName, b.FullName, isAsc);
default:
return 0;
}
});
This is working as intended, however I want to sort the date property the same way, in a chronological way. How can I do that? I basically just need a working compare function, which supports 'isAsc' - is True for ascending and false for descending.
The a.Orderdate variable can be parsed to a date with
new Date(a.OrderDate)
Since it is a date converted to a string.