Skip to main content

How to Sort an Array of Objects by a Date Property in JavaScript

Sorting an array of objects based on a date is a fundamental task in data manipulation, essential for ordering blog posts, financial transactions, or events chronologically. The standard method for this is Array.prototype.sort(), which can easily handle date comparisons with a custom compare function.

This guide will teach you the modern, standard method for sorting an array of objects by a date property. You will learn how to sort in both ascending and descending order and how to handle date properties that are stored as strings instead of Date objects.

The Core Method: Array.prototype.sort()

The sort() method sorts the elements of an array. To sort objects, you must provide a compare function that tells sort() how to determine the correct order between any two objects. For dates, the easiest way to do this is by comparing their numeric timestamp values.

array.sort((a, b) => a.date - b.date)

When you subtract one Date object from another, JavaScript automatically coerces them into their millisecond timestamp values, making the comparison simple and efficient.

How to Sort by Date Objects

Let's start with the ideal scenario, where your date properties are already Date objects.

Problem: you have an array of events and want to sort them chronologically.

// Problem: How to sort this array by the `date` property?
let events = [
{ name: 'Event A', date: new Date('2025-10-27') },
{ name: 'Event C', date: new Date('2025-10-29') },
{ name: 'Event B', date: new Date('2025-10-28') },
];

Ascending Order (Oldest to Newest)

To sort in ascending order, you subtract the date of the first object (a) from the date of the second object (b).

let events = [
{ name: 'Event A', date: new Date('2025-10-27') },
{ name: 'Event C', date: new Date('2025-10-29') },
{ name: 'Event B', date: new Date('2025-10-28') },
];

events.sort((a, b) => a.date - b.date);

console.log(events);

Output:

[
{name: 'Event A', date: Mon Oct 27 2025 00:00:00 GMT...},
{name: 'Event B', date: Tue Oct 28 2025 00:00:00 GMT...},
{name: 'Event C', date: Wed Oct 29 2025 00:00:00 GMT...}
]

Descending Order (Newest to Oldest)

To sort in descending order, you simply reverse the subtraction.

let events = [
{ name: 'Event A', date: new Date('2025-10-27') },
{ name: 'Event C', date: new Date('2025-10-29') },
{ name: 'Event B', date: new Date('2025-10-28') },
];

events.sort((a, b) => b.date - a.date);

console.log(events);

Output:

[
{name: 'Event C', date: Wed Oct 29 2025 00:00:00 GMT...}
{name: 'Event B', date: Tue Oct 28 2025 00:00:00 GMT...}
{name: 'Event A', date: Mon Oct 27 2025 00:00:00 GMT...}
]

A Best Practice: Sorting Immutably

A critical characteristic of the sort() method is that it mutates (modifies) the original array in place. This can lead to bugs in larger applications where the original order might be needed elsewhere.

The best practice is to create a copy of the array before sorting it. The modern spread syntax (...) makes this easy.

let originalEvents = [ 
{ name: 'Event A', date: new Date('2025-10-27') },
{ name: 'Event C', date: new Date('2025-10-29') },
{ name: 'Event B', date: new Date('2025-10-28') }
];

// Create a shallow copy before sorting
let sortedEvents = [...originalEvents].sort((a, b) => a.date - b.date);

console.log(originalEvents); // The original array is still in its original order
console.log(sortedEvents); // This new array is sorted

Output:

[ 
{ name: 'Event A', date: Mon Oct 27 2025 01:00:00 GMT... },
{ name: 'Event C', date: Wed Oct 29 2025 00:00:00 GMT... },
{ name: 'Event B', date: Tue Oct 28 2025 00:00:00 GMT... }
]
[
{ name: 'Event A', date: Mon Oct 27 2025 01:00:00 GMT... },
{ name: 'Event B', date: Tue Oct 28 2025 00:00:00 GMT... },
{ name: 'Event C', date: Wed Oct 29 2025 00:00:00 GMT... }
]

Handling Date Properties Stored as Strings

In the real world, you often receive data from APIs where dates are formatted as strings (e.g., YYYY-MM-DD). The sort() method will not work correctly on these strings directly, as it would perform an alphabetical sort, not a chronological one.

Example of problem:

// Problem: Sorting this array alphabetically will give the wrong result.
let posts = [
{ title: 'Post 1', date: '2025-10-27' },
{ title: 'Post 3', date: '2025-11-05' },
{ title: 'Post 2', date: '2025-10-28' },
];

Solution: the solution is to convert the date strings into Date objects inside the compare function before performing the subtraction.

let posts = [
{ title: 'Post 1', date: '2025-10-27' },
{ title: 'Post 3', date: '2025-11-05' },
{ title: 'Post 2', date: '2025-10-28' },
];

let sortedPosts = [...posts].sort((a, b) => {
// Convert the date strings to Date objects before comparison
return new Date(a.date) - new Date(b.date);
});

console.log(sortedPosts);

Output:

[
{title: 'Post 1', date: '2025-10-27'},
{title: 'Post 2', date: '2025-10-28'},
{title: 'Post 3', date: '2025-11-05'}
]
note

This is a robust approach because the new Date() constructor reliably parses the standard YYYY-MM-DD format.

Conclusion

Sorting an array of objects by a date property is a simple task if you use the correct compare function with Array.prototype.sort().

  • The recommended best practice is to subtract the Date objects directly within the compare function: array.sort((a, b) => a.date - b.date).
  • For descending order, reverse the subtraction: array.sort((a, b) => b.date - a.date).
  • To avoid bugs and follow modern best practices, sort immutably by creating a copy of the array first: let sorted = [...myArray].sort(...).
  • If your dates are strings, convert them to Date objects inside the compare function: new Date(a.date) - new Date(b.date).