Cómo ordenar una matriz usando Array.prototype.sort() por type y currency en este orden:
débito => crédito => externo => préstamo
y si hay varias monedas del mismo tipo, entonces organícelas en este orden:
RUB => USD => EUR => GBP
Entiendo cómo usar ordenar () con números, pero no entiendo cómo hacer esa clasificación.
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) => { });Puede almacenar el orden de las propiedades de type y currency en una matriz y luego restar los índices para determinar la precedencia:
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)Puede tomar objetos para el pedido y encadenar los deltas con OR lógico.
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; }Puede crear dos mapas para almacenar la "prioridad" de cada categoría, y el resultado de la clasificación sería la diferencia entre los tipos de prioridades, o la de las monedas si 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);