I'm trying to do an array sorting, it's already working.
This code:
search = async() => {
const response = await api.post('/client/find', { "filter": this.state.filterSelected, "input": this.state.textSearch });
// ordenar de acordo com o select
var comparador = this.state.filterSelected.toLowerCase();
this.setState({ docs: response.data.sort((a, b) => {
if (a.name < b.name) return -1; // sort string ascending
if (a.name > b.name) return 1;
return 0; // default return value (no sorting)
})});
this.setState({docs: response.data});
}
It takes an array of the API (in a MongoDB database) and shows it on the screen, in an array ordered by name.
The problem is that I wanted to do this according to a select of the selected page, the comparator variable, already returns the name, but it would not work puts the commaster, because it went behind comparator, not name, even though passing the name value in the comparator variable Would you like to pass name in the value of the comparator variable and use it in ifs? if (a partition) return -1;