I have this piece of code written as a function to handle a button-toggle for sorting a list in ascending and descending order. Below is the code which is working as expected, but I would appreciate if someone knows a better, shorter and cleaner way of writing it.
// method called every time the button is clicked
// it will change the sorting order
//(ASCENDING => DESCENDING => ASCENDING => DESCENDING) in that order
onSortChange = () => {
this.setState({
users: this.state.order
? this.state.users.sort((a, b) => {
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
})
: this.state.users.reverse((a, b) => {
if (a.name < b.name) return 1;
if (a.name > b.name) return -1;
return 0;
}),
order: !this.state.order,
});
}
```