How to Sort an Array by Date in JavaScript

Updated onbyAlan Morel
How to Sort an Array by Date in JavaScript

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:

const dates = [
    new Date("2023-01-01"),
    new Date("2020-01-01"),
    new Date("2021-01-01")
];

console.log(dates);
0: 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:

const 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);
0: 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:

const array = [
    { date: new Date("2023-01-01") },
    { date: new Date("2020-01-01") },
    { date: new Date("2021-01-01") }
];

console.log(array);
0: {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:

const 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);
0: {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!

To learn more about web development, founding a start-up, and bootstrapping a SaaS, follow me on X!
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.