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

209
Views
How to sort an array using Array.prototype.sort() by two properties?

How to sort an array using Array.prototype.sort() by type and currency in this order:

debit => credit => external => loan

and if there are several currencies in the same type, then arrange in this order:

RUB => USD => EUR => GBP

I understand how to use sort () with numbers, but I don’t understand how to do such sorting.

const accounts = [
  { id: 1, type: 'credit', currency: 'GBP' },
  { id: 2, type: 'debit', currency: 'EUR' },
  { id: 3, type: 'loan' },
  { id: 4, type: 'credit', currency: 'RUB' },
  { id: 5, type: 'credit', currency: 'RUB' },
  { id: 6, type: 'debit', currency: 'USD' },
  { id: 7, type: 'debit', currency: 'RUB' },
  { id: 8, type: 'external' },
  { id: 9, type: 'credit', currency: 'EUR' }
];

accounts.sort((a, b) => {
  
});

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

0

You can store the order of the type and currency properties in an array, then subtract the indexes to determine precedence:

const typeOrder = ['debit', 'credit', 'external', 'loan']
const currencyOrder = ['RUB', 'USD', 'EUR', 'GBT']

const accounts=[{id:1,type:"credit",currency:"GBP"},{id:2,type:"debit",currency:"EUR"},{id:3,type:"loan"},{id:4,type:"credit",currency:"RUB"},{id:5,type:"credit",currency:"RUB"},{id:6,type:"debit",currency:"USD"},{id:7,type:"debit",currency:"RUB"},{id:8,type:"external"},{id:9,type:"credit",currency:"EUR"}];accounts.sort((e,c)=>{});

const res = accounts.sort((a,b) => {
  return typeOrder.indexOf(a.type) - typeOrder.indexOf(b.type) || currencyOrder.indexOf(a.currency) - currencyOrder.indexOf(b.currency)
})

console.log(res)

about 4 years ago · Juan Pablo Isaza Report

0

You could take objects for the order and chain the deltas with logical OR.

const
    types = { debit: 1, credit: 2, external: 3, loan: 4 },
    currencies = { RUB: 1, USD: 2, EUR: 3, GBT: 4 },
    accounts = [{ id: 1, type: 'credit', currency: 'GBP' }, { id: 2, type: 'debit', currency: 'EUR' }, { id: 3, type: 'loan' }, { id: 4, type: 'credit', currency: 'RUB' }, { id: 5, type: 'credit', currency: 'RUB' }, { id: 6, type: 'debit', currency: 'USD' }, { id: 7, type: 'debit', currency: 'RUB' }, { id: 8, type: 'external' }, { id: 9,
type: 'credit', currency: 'EUR' }];

accounts.sort((a, b) => 
    types[a.type] - types[b.type] ||
    currencies[a.currency] - currencies[b.currency]
);

console.log(accounts);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

You can create two maps to store the "priority" of every category, and the sort result would be the difference between the types priorities, or that of the currencies if 0:

const accounts = [ { id: 1, type: 'credit', currency: 'GBP' }, { id: 2, type: 'debit', currency: 'EUR' }, { id: 3, type: 'loan' }, { id: 4, type: 'credit', currency: 'RUB' }, { id: 5, type: 'credit', currency: 'RUB' }, { id: 6, type: 'debit', currency: 'USD' }, { id: 7, type: 'debit', currency: 'RUB' }, { id: 8, type: 'external' }, { id: 9, type: 'credit', currency: 'EUR' } ];

const 
 TYPE_PRIORITY = { 'debit': 1, 'credit': 2, 'external': 3, 'loan': 4 },
 CURRENCY_PRIORITY = { 'RUB': 1, 'USD': 2, 'EUR': 3, 'GBP': 4 };
accounts.sort((a, b) => 
  TYPE_PRIORITY[a.type] - TYPE_PRIORITY[b.type] || 
  CURRENCY_PRIORITY[a.currency] - CURRENCY_PRIORITY[b.currency]
);

console.log(accounts);

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!