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

270
Views
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
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!