Skip to content

Instantly share code, notes, and snippets.

@djmin43
Created May 30, 2021 10:46
Show Gist options
  • Save djmin43/489a0105261fb950f231c9d57edaa78d to your computer and use it in GitHub Desktop.
Save djmin43/489a0105261fb950f231c9d57edaa78d to your computer and use it in GitHub Desktop.
// SORT BY DATE (TOGGLE)
const sortByDate = () => {
if (new Date(inventory[0].expiration_date).valueOf() < new Date(inventory[inventory.length -1 ].expiration_date).valueOf()) {
const sortedInventory = inventory.slice(0).sort((a: Inventory, b:Inventory) => {return new Date(b.expiration_date).valueOf() - new Date(a.expiration_date).valueOf()})
setInventory(sortedInventory)
} else {
const sortedInventory = inventory.slice(0).sort((a: Inventory, b:Inventory) => {return new Date(a.expiration_date).valueOf() - new Date(b.expiration_date).valueOf()})
setInventory(sortedInventory)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment