Right now I have a table with some data from the github api. In the table you can click on the Stars heading and it sorts the list from 0 to *. My function works for one column. But how can I use this function over and over again for different columns?
My table headings :
<th>Name</th>
<th onClick={this.sortList}>Stars</th>
{* This should update value forks_count *}
<th onClick={this.sortList}>Forks</th>
My function:
What I do is I get my array and sort it based on stargazers_count. This works, however when I want to sort my Forks count.. stargazers_count should be forks_count. Is this possible?
sortList = () => {
const items = this.props.repos;
items.sort(function (a, b) {
//stargazers_count should be forks_count when I click on forks heading
return a.stargazers_count - b.stargazers_count;
});
this.setState({
repos: items
})
};