Christian
Christian

Reputation: 73

Javascript sort by multiple categories

I need to make a sort by Status and ETA, I've managed to make the sort by Status by can't figure out to make it also by ETA. What I've done so far is only sorting by Status, it should sort by ETA also, but I can't manage to figure out why this solution is not working.

Here is my code:

const data = [
        {
          id: 1,
          name: "Delivery 1",
          amount: 3,
          status: "active",
          eta: 15
        },
        {
          id: 2,
          name: "Delivery 2",
          amount: 5,
          status: "pending"
        },
        {
          id: 3,
          name: "Delivery 3",
          amount: 3,
          status: "active",
          eta: 10
        },
        {
          id: 4,
          name: "Delivery 4",
          amount: 4,
          status: "upcoming"
        },
        {
          id: 5,
          name: "Delivery 5",
          amount: 3,
          status: "active",
          eta: 25
        },
        {
          id: 6,
          name: "Delivery 6",
          amount: 3,
          status: "active",
          eta: 5
        }
      ];

<tbody>
  {searchResults.sort((a, b) => a.eta > b.eta ? 1 : -1 && statusOrdered.indexOf(a.status) - statusOrdered.indexOf(b.status)).map(el => {
    return (
    <tr>
      <td>{el.id}</td>
      <td>{el.name}</td>
      <td>{el.amount}</td>
      <td>{el.eta}</td>
      <td>{el.status}</td>
    </tr> 
    );
  })}
  </tbody>
    </table>
  );
};

Upvotes: 0

Views: 158

Answers (1)

Maradox
Maradox

Reputation: 640

Just to explain you how it will look like, consider the following example.

(a.eta - b.eta) || (a.status - b.status)

So you need to use || instead of &&.

Upvotes: 1

Related Questions