I was doing React add list and sorting, but the sorting is not working perfectly. it sort well except the first elements. when i add aa, bb, cc elements in order, it should sort as cc, bb,aa, but it actually sort as aa, cc, bb. When i add other elements, it works well except first element 'aa'
I am working on this for two days but still don't know what's the problem... please help me
My sort function is like this,
handleList = () => {
const { information } = this.state;
information.sort((a, b) => (a.name > b.name ? -1 : 1));
this.setState({
information: information,
});
};
My state, and handleCreate function
id = 0;
state = {
sortingState: false,
information: [],
};
handleCreate = (data) => {
const { information } = this.state;
this.setState({
information: information.concat({ id: this.id++, ...data }),
});
};
render
render() {
const { information } = this.state;
return (
<div>
<Input onCreate={this.handleCreate} />
<DataList
data={information}
onRemove={this.handleRemove}
onUpdate={this.handleUpdate}
/>
<button onClick={this.handleList}>Sort</button>
</div>
);
}