Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!