1

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) => {
  
});

0

4 Answers 4

2

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)

Sign up to request clarification or add additional context in comments.

Comments

1

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; }

Comments

1

This is the simplest

When order1 is 0 (a and b are the same) we use order2

const order1 = ["debit","credit","external","loan"],
order2 = ["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((a, b) => { 
  return order1.indexOf(a.type) - order1.indexOf(b.type) || 
  order2.indexOf(a.currency) - order2.indexOf(b.currency)
});
console.log(accounts)

Comments

1

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.