This tutorial will help you learn about how to sort an array of objects by date property using JavaScript. Javascript offers you some methods to do it. We already tested effectively by using the sort()
function with the Date()
object. Hopefully, throughout this article, you can find for yourself the best way to sort an array of objects by date. Read till the end of the article to see how we use it.
To Sort An Array Of Objects By Date Property In JavaScript
Using The Array sort()
method
JavaScript provides the sort()
function to sort the object, which allows developers to put the
Date objects in an array of objects and arranges the dates in any order. The sort()
function will help us sort the elements of an array and will overwrite the original array. To more detail about this function. We already have an article that discusses how to utilize this function in further depth. Please read it here.
Code Example
In this example, we assort an array of objects by date property following this way.
- First, we create an array of an object named “schedule” that stores object containing Date objects.
- We subtract two date objects.
- After that, call out the
sort()
method on an array.
Let’s see the code example below.
const schedule = [ { id: 1, date: new Date("2022-12-23"), title: "Meeting with customer" }, { id: 2, date: new Date("2022-12-17"), title: "Go to shopping" }, { id: 3, date: new Date("2022-12-27"), title: "Go to fishing" }, ]; // Using the sort() function schedule.sort(function (a, b) { // subtract the date value return b.date - a.date; }); console.log("After sorting by date in descending order", schedule);
Output
After sorting by date
(3) [{...}, {...}, {...}]
0:(3) {id: 3, date: Tue Dec 27 2022 07:00:00 GMT, title: "Go to shopping"}
1:(3) {id: 1, date: Fri Dec 23 2022 07:00:00 GMT, title: "Meeting with customer"}
2:(3) {id: 2, date: Sat Dec 17 2022 07:00:00 GMT, title: "Go to fishing"}
Create the customSort
with the sort()
method
The second way we want to introduce this article is by creating the customSort
function. In this function, we will create the two date object and compare them together. Then, we use the sort()
method with customSort
function to organize all elements in an array of the object by using the Date()
object.
Code Example
const schedule = [ { id: 1, date: new Date("2022-12-23"), title: "Meeting with customer" }, { id: 2, date: new Date("2022-12-17"), title: "Go to shopping" }, { id: 3, date: new Date("2022-12-27"), title: "Go to fishing" }, ]; // Create customSort to sort descending order customSort = (a, b) => { const date1 = new Date(a.date); const date2 = new Date(b.date); if (date1 > date2) { return -1; } else if (date1 < date2) { return 1; } else { return 0; } }; console.log( "After sorting by date in descending order", schedule.sort(customSort) );
Output
After sorting by date
(3) [{...}, {...}, {...}]
0:(3) {id: 3, date: Tue Dec 27 2022 07:00:00 GMT, title: "Go to shopping"}
1:(3) {id: 1, date: Fri Dec 23 2022 07:00:00 GMT, title: "Meeting with customer"}
2:(3) {id: 2, date: Sat Dec 17 2022 07:00:00 GMT, title: "Go to fishing"}
Summary
To summarize, you will be an expert in sorting an array of objects by date property in Javascript after using the two techniques I’ve described called the sort()
function in an array and subtracting the two date objects. If you have any comments on the article, leave me a comment below.
Thank you for your reading!
Maybe you are interested:
- Sort an Array of Objects by Boolean property in JavaScript
- Sort an Array with NULL values coming last in JavaScript
- Sort an Array of Booleans in JavaScript

My name is Fred Hall. My hobby is studying programming languages, which I would like to share with you. Please do not hesitate to contact me if you are having problems learning the computer languages Java, JavaScript, C, C#, Perl, or Python. I will respond to all of your inquiries.
Name of the university: HUSC
Major: IT
Programming Languages: Java, JavaScript, C , C#, Perl, Python