JavaScript

Sort an Array of Objects by Date Property in JavaScript

Sometimes, developers store Date objects in an array of objects in random sequence, and they want to sort the dates in any specific order, such as ascending order or descending order. To do this, JavaScript provides the sort() method for sorting the objects.

This tutorial will describe the procedure for sorting the array of objects by Date property in JavaScript.

How to Sort JavaScript Array of Objects by Date Property?

For sorting objects in an array by Date property, subtract the two dates by calling the “sort()” method on an array. The array is modified when sorted in place using the sort() method and returned as the sorted array. More specifically, when two values are compared, the sort() method passes the values to the compare function and orders the values based on the (negative, zero, positive) result it returns.

Syntax

Follow the given syntax for sorting the array of objects:

sort((a, b) => { a.date - b.date} )

Here, in the arrow function, subtract the first date object from the second date object. It returns output in (+ve, -ve, or 0). If the resultant value is -ve, “a” is ordered before “b”. If the result is +ve, “b” is sorted ahead of “a”.

Example 1: Sort an Array of Objects in Ascending Order by Date Property Using sort() Method

In this example, we will sort an array of objects in ascending order. First, create an array of objects named “array” that stores three objects containing Date objects with ids:

const array = [

{id: 5, date: new Date(2008, 2, 23)},

{id: 11, date: new Date(2010, 11, 20)},

{id: 15, date: new Date(2000, 10, 08)},

];

Call the “sort()” method to sort an array of objects by a date property using the arrow function:

const ascSort = array.sort((obj1, obj2) =>

obj1.date - obj2.date,

);

Finally, print the sorted array on the console:

console.log(ascSort);

The output indicates that the Date objects are sorted in ascending order:

Example 2: Sort an Array of Objects in Descending Order by Date Property Using sort() Method

For sorting the array in descending order, we will subtract the second date object from the first date object:

const ascSort = array.sort((obj1, obj2) =>

obj2.date - obj1.date,

);

Output

We have gathered all the necessary information relevant to sort the array of objects by Date property in JavaScript.

Conclusion

For sorting an array of objects by Date property, use the “sort()” method by calling it on an array and subtracting the two date objects. When two values are compared, the sort() method passes the values to the compare function and orders the values based on the (negative, zero, positive) result it returns. In this tutorial, we described the procedure for sorting the array of objects by Date property in JavaScript.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.