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 TypeScript
- How to Install Node on Windows, macOS and Linux
- Getting Started with Express
- Create an RSS Reader in Node
- Git Tutorial: Learn how to use Version Control
- How to deploy a PHP app using Docker
- How to deploy a Deno app using Docker
- Learn how to use v-model with a custom Vue component
- Getting Started with Handlebars.js
- Getting User Location using JavaScript's Geolocation API
- Learn how to build a Slack Bot using Node.js
- Getting Started with Vuex: Managing State in Vue