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