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

112
Views
Javascript custom sort

I've implement this sort:

<tbody>
  {results.sort((a, b) => a.status > b.status ? 1 : -1).map(el => {
    return (
    <tr>
      <td>{el.id}</td>
      <td>{el.amount}</td>
      <td>{el.time}</td>
      <td>{el.status}</td>
     
    </tr> 
    );
  })}
  </tbody>

So now I have them sorted by status as follows: active, updated, waiting. The problem is that I want them sorted by active, waiting, updated. How can I achieve this?

This is my array of objects:

 const data = [
        {
          id: 1,
          title: "Order 1",
          amount: 3,
          status: "active"
        },
        {
          id: 2,
          title: "Order 2",
          amount: 5,
          status: "waiting"
        },
        {
          id: 3,
          title: "Order 3",
          amount: 4,
          status: "updated"
        },
        {
          id: 4,
          title: "Order 4",
          amount: 3,
          status: "active"
        }
      ];
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use indexOf to an ordered array, with this approach:

const data = [{
    id: 1,
    title: "Order 1",
    amount: 3,
    status: "active"
  },
  {
    id: 2,
    title: "Order 2",
    amount: 5,
    status: "waiting"
  },
  {
    id: 3,
    title: "Order 3",
    amount: 4,
    status: "updated"
  },
  {
    id: 4,
    title: "Order 4",
    amount: 3,
    status: "active"
  }
];

const statusOrdered = ['active', 'waiting', 'updated']

data.sort((a, b) => statusOrdered.indexOf(a.status) - statusOrdered.indexOf(b.status));

console.log(data)

about 4 years ago · Juan Pablo Isaza Report

0

Try something like this for your sorting function.

(a,b) => {
    let ranking = {
        active:1,
        waiting:2,
        updated:3,
    }
    return ranking[a.status] - ranking[b.status]
}
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!