Reputation: 314
I'm using Sorttable in my React application to make the headers of my table sortable. It's functioning as expected but I'd like to be able to set the default sort to a specific header. I've checked the documentation here and have also attempted this solution from a stack overflow response on a similar question. I was unable to get the default sort to work. Any advice on how I can accomplish this in React; either using sorttable or otherwise? Thanks!
Upvotes: 0
Views: 132
Reputation: 314
I was able to set the default sort using the .sort() method on my array.
Here is a function to sort the properties. You can change the return order from a to b if you'd like to have ascending vs descending order.
function dynamicSort(property) {
return function (a, b) {
return b[property] - a[property];
};
}
Then use the sort() method to call the function with the name of the property you'd like to sort by.
trucks.sort(dynamicSort("status"));
Upvotes: 0