Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

198
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda