How to Sort an Array by Date in JavaScript
Table of Contents
When you have an array containing dates, it is common to need the array sorted by date.
This is useful in knowing which of the dates came first and which came last.
In this post, we'll learn how to sort an array of dates and an array of objects containing a date property.
How to sort an array of dates
Sorting an array of dates is extremely simple when you use the built-in array sort method.
This method takes a callback function as an argument, which determines which element should come first.
Thankfully, we can just subtract the two dates and compare the result.
If the result is positive, the first date comes first, and if the result is negative, the second date comes first.
Let's start off without our date array:
JAVASCRIPTconst dates = [
new Date("2023-01-01"),
new Date("2020-01-01"),
new Date("2021-01-01")
];
console.log(dates);
BASH0: Sat Dec 31 2022 19:00:00 GMT-0500 (Eastern Standard Time) {}
1: Tue Dec 31 2019 19:00:00 GMT-0500 (Eastern Standard Time) {}
2: Thu Dec 31 2020 19:00:00 GMT-0500 (Eastern Standard Time) {}
Now let's sort the array:
JAVASCRIPTconst dates = [
new Date("2023-01-01"),
new Date("2020-01-01"),
new Date("2021-01-01")
];
dates.sort((date1, date2) => date1 - date2);
console.log(dates);
BASH0: Tue Dec 31 2019 19:00:00 GMT-0500 (Eastern Standard Time) {}
1: Thu Dec 31 2020 19:00:00 GMT-0500 (Eastern Standard Time) {}
2: Sat Dec 31 2022 19:00:00 GMT-0500 (Eastern Standard Time) {}
As expected, the dates were sorted from earliest to latest.
How to sort an array of objects containing a date property
This time, let's define an array of objects, each containing a date property:
JAVASCRIPTconst array = [
{ date: new Date("2023-01-01") },
{ date: new Date("2020-01-01") },
{ date: new Date("2021-01-01") }
];
console.log(array);
BASH0: {date: Sat Dec 31 2022 19:00:00 GMT-0500 (Eastern Standard Time)}
1: {date: Tue Dec 31 2019 19:00:00 GMT-0500 (Eastern Standard Time)}
2: {date: Thu Dec 31 2020 19:00:00 GMT-0500 (Eastern Standard Time)}
Now, let's call the properties inside the sort method:
JAVASCRIPTconst array = [
{ date: new Date("2023-01-01") },
{ date: new Date("2020-01-01") },
{ date: new Date("2021-01-01") }
];
array.sort((a, b) => a.date - b.date);
console.log(array);
BASH0: {date: Tue Dec 31 2019 19:00:00 GMT-0500 (Eastern Standard Time)}
1: {date: Thu Dec 31 2020 19:00:00 GMT-0500 (Eastern Standard Time)}
2: {date: Sat Dec 31 2022 19:00:00 GMT-0500 (Eastern Standard Time)}
The same principle applies to sorting arrays of objects containing a date property in that we just need to subtract the two dates and compare the result inside the callback function.
The objects are now sort as expected by their dates.
Conclusion
In this post, we learned how to sort an array of dates and an array of objects containing a date property.
Simply use the sort
method and pass it a callback function that compares the dates and let the method do all the work for you.
Thanks for reading and happy coding!
- Getting Started with Solid
- Getting Started with Svelte
- Getting Started with Express
- Git Tutorial: Learn how to use Version Control
- How to Set Up Cron Jobs in Linux
- How to deploy a Deno app using Docker
- How to deploy a MySQL Server using Docker
- How to deploy a Node app using Docker
- How to Scrape the Web using Node.js and Puppeteer
- Build a Real-Time Chat App with Node, Express, and Socket.io
- Getting Started with Moment.js
- Building a Real-Time Note-Taking App with Vue and Firebase