I generate cards on a button press. These cards have a randomly generated number between 0 and 100. I am trying to setup a component or function that would allow me to sort all of these cards in numerical order ascending or descending, or both, when I click the sort button. I have tried the following code. Keep in mind, this is all contained within the same App component.
The random numbers are generated in the add card component.
I feel like I'm super close, but I can't figure out what I'm missing.
const sortTypes = {
up: {
class: "sort-up",
fn: (a, b) => a.number - b.number,
},
down: {
class: "sort-down",
fn: (a, b) => b.number - a.number,
},
default: {
class: "sort",
fn: (a, b) => a,
},
};
const sortAll = () => {
state = {
currentSort: 'default'
};
onSortChange = () => {
const { currentSort } = this.state;
let nextSort;
if (currentSort === 'down') nextSort = 'up';
else if (currentSort === 'up') nextSort = 'default';
else if (currentSort === 'default') nextSort = 'down';
this.setState({
currentSort: nextSort
});
};
};
return (
<body>
<header>
<div className="ui buttons">
<button type="button" onClick={addCard} className="ui button mb-1 mt-1 mr-1"><i className="plus icon"></i>Add Card</button>
<div className="or mb-1 mt-1"></div>
<button type="button" onClick={sortAll} className="ui positive button mb-1 mt-1 mr-1"><i className="redo icon"></i>Sort All</button>
</div>
</header>