Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hassanuos/8530095699d457cdfc0c4832d66983be to your computer and use it in GitHub Desktop.
Save hassanuos/8530095699d457cdfc0c4832d66983be to your computer and use it in GitHub Desktop.
Sort Array of JSON Object by date values
// Sort array of JSON objects by date value
const records = [
{
order_id: 12345,
order_date: "2020-03-23"
},
{
order_id: 12346,
order_date: "2020-03-20"
},
{
order_id: 12347,
order_date: "2020-03-22"
}
]
records.sort((a, b) => {
return new Date(a.order_date) - new Date(b.order_date); // descending
})
records.sort((a, b) => {
return new Date(b.order_date) - new Date(a.order_date); // ascending
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment