• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

87
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

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

almost 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda