I have an array of objects with a date format that looks this way:
"offline_available": "5/1/2021"
As I understand it is the date formatted by the toLocaleDateString()
method.
How can I sort all objects by next available date?
I have an array of objects with a date format that looks this way:
"offline_available": "5/1/2021"
As I understand it is the date formatted by the toLocaleDateString()
method.
How can I sort all objects by next available date?
Start with this - assuming the date is mm/dd/yyyy
const arr = [
{"offline_available": "5/1/2021"},
{"offline_available": "12/13/2020"},
{"offline_available": "5/3/2020"},
{"offline_available": "5/3/2021"}
]
const sorted = arr.slice(0).sort((a,b) => new Date(a.offline_available) - new Date(b.offline_available))
console.log(sorted)
What is "sort by next available date"?
You need to translate your date string into a date object in the comparison function.
yourArray.sort( (a,b) => new Date(a.offline_available) - new Date(b.offline_available))