0

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.

ivan239
  • 21
  • 2

0 Answers0