Presenting an Array of Objects with various transactions:
[{
date: 2022-04-07T01:00:00.000+00:00,
type: 'profit'
amount: 200
},{
date: 2022-04-07T01:00:00.000+00:00,
type: 'withdraw'
amount: 600
},{
date: 2022-04-07T01:00:00.000+00:00,
type: 'invest'
amount: 900
},{
date: 2022-04-08T01:00:00.000+00:00,
type: 'deposit'
amount: 200
},{
date: 2022-04-08T01:00:00.000+00:00,
type: 'profit'
amount: 200
}]
The dates in the data source are unordered, so a sorting operation by date is necessary like this:
this.allTransactions.sort((a: any, b: any) => {
return new Date(b.date).getTime() - new Date(a.date).getTime();
});
In addition to sorting by date, there is a requirement to sort objects with the same date according to an external array:
['invest', 'withdraw', 'profit']
A challenge arises in implementing the secondary sorting for objects within the same date group based on the order defined in the array mentioned. The array this.allTransactions
is part of an *ngFor
loop within Angular 12.