Skip to content

Instantly share code, notes, and snippets.

@Ateevduggal
Created June 11, 2022 05:15
Show Gist options
  • Save Ateevduggal/ab1c42d21be1c69a2944c709552d7bd2 to your computer and use it in GitHub Desktop.
Save Ateevduggal/ab1c42d21be1c69a2944c709552d7bd2 to your computer and use it in GitHub Desktop.
const Sort = (sort) => {
if (order === "asc") {
const sorted = [...tableData.sort((a, b) => (a[sort] > b[sort] ? 1 : -1))];
console.log(sorted);
setTableData(sorted);
setOrder("desc"); // descending order - desc
} else if (order === "desc") {
const sorted = [...tableData.sort((a, b) => (a[sort] < b[sort] ? 1 : -1))];
console.log(sorted);
setTableData(sorted);
setOrder("asc");
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment