Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
Sort by Date and OrderBy external Array

I have an Array of Objects like this:

[{
  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
}]

In the datasource the dates are not in order so I am sorting by date like this:

this.allTransactions.sort((a: any, b: any) => {
  return new Date(b.date).getTime() - new Date(a.date).getTime();
});

However I need to also sort any objects with the same date by an external array:

['invest', 'withdraw', 'profit']

I'm struggling with the second part sorting the date groups already sorted by the order in the array. this.allTransactions is in an *ngFor loop in Angular 12

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can add an extra condition after your current one, which will be used if the first one resolves as 0

const typeOrder = ['invest', 'withdraw', 'profit']

transactions.sort((a, b) => {
  return new Date(b.date).getTime() - new Date(a.date).getTime() || typeOrder.indexOf(a.type) - typeOrder.indexOf(b.type)
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!