Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

208
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda