How to sort a map by a specific field in es6 / React?
Assuming id
is a number you can do products.sort(({id: previousID}, {id: currentID}) => previousID - currentID)
Like so:
JavaScript Code:
{products
.sort(({ id: previousID }, { id: currentID }) => previousID - currentID)
.map(({ id, headline }) => (
<Container key={id}>
<Row>
<Col>
<p>id={id}</p>
<p>headline={headline}</p>
</Col>
</Row>
</Container>
))
}
You could sort
the array before you map
it:
{products.sort((a, b) => a.id - b.id).map(({ id, headline }) => (
<Container>
<Row key={id}>
<Col>
<p>id={id}</p>
<p>headline={headline}</p>
</Col>
</Row>
</Container>
))}