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

123
Vistas
How to sort by order in another array?

I have the first array that be like

status = [
{title: 'pending', order: 1},
{title: 'success', order: 2}
]

And I have the second array that be like

task = [
{status: 'success', price: 100},
{status: 'success', price: 150},
{status: 'pending', price: 100}, 
{status: 'success', price: 300}
]

How Can I sort This for results be like

task = [
{status: 'pending', price: 100},
{status: 'success', price: 100},
{status: 'success', price: 150},
{status: 'success', price: 300}
]

sort by order of status from the first array

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can easily achieve the result using Map and by passing the comparator function to sort method

const status = [
  { title: "pending", order: 1 },
  { title: "success", order: 2 },
];

const task = [
  { status: "success", price: 100 },
  { status: "success", price: 150 },
  { status: "pending", price: 100 },
  { status: "success", price: 300 },
];

const dict = new Map(status.map(o => [o.title, o.order]));
const result = [...task].sort((a, b) => dict.get(a.status) - dict.get(b.status));

console.log(result);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

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