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

272
Visualizações
How to realize specific sort function?

I have accounts array of objects:

accounts[{type: "debit", currency: "EUR"}{type, currency}{type, currency}...]

And I need to sort accounts array in next order: debit -> credit -> external -> saving -> loan. If there are several accounts with one type then sorting according to currency: RUB -> USD -> EUR -> GBP. I have:

accounts.sort(function(a, b) {
        let aStatus = 1;
        let bStatus = 1;
        if (a.type === "credit") {
            aStatus = 5;
        }
        if (a.type === "external") {
            aStatus = 9;
        }
        if (a.type === "saving") {
            aStatus = 13;
        }
        if (a.type === "loan") {
            aStatus = 17;
        }
        if (b.type === "credit") {
            bStatus = 5;
        }
        if (b.type === "external") {
            aStatus = 9;
        }
        if (b.type === "saving") {
            bStatus = 13;
        }
        if (b.type === "loan") {
            bStatus = 17;
        }
        if (a.currency === "USD") {
            aStatus += 1;
        }
        if (a.currency === "EUR") {
            aStatus += 2;
        }
        if (a.currency === "GBP") {
            aStatus += 3;
        }
        if (b.currency === "USD") {
            bStatus += 1;
        }
        if (b.currency === "EUR") {
            bStatus += 2;
        }
        if (b.currency === "GBP") {
            bStatus += 3;
        }
        return aStatus - bStatus;
    })

First problem: it's not working. Second: it's seems not rationality and huge. Help me please to make better code.

about 4 years ago · Juan Pablo Isaza
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