Scenario
Have have the following Typescript class instances:
class DateClass {
constructor(public name: string, public receiptDate: Date) {}
}
const d1 = new DateClass('d1', new Date(3));
const d2 = new DateClass('d2', new Date(2));const arr: DateClass[] = [d1, d2];
And we wish to sort them in the following order:
[d2, d1]
Approach
const sorted = arr.sort((a, b) => {return a.receiptDate.getTime() - b.receiptDate.getTime();});console.log(sorted);