Sort an Object Array by Date in JavaScript
To sort an Object Array by Date in JavaScript, we have different approaches. We are going to learn how to sort an Object Array by Date in JavaScript.
Below are the approaches to sort an Object Array by Date in JavaScript:
1. Using sort method with Date objects
This approach uses the built-in sort
method along with Date
objects. The sort
method compares Date
objects directly, sorting the array in ascending order based on the date property.
const data = [
{ name: 'Event 1', date: new Date('2023-01-15') },
{ name: 'Event 2', date: new Date('2022-12-20') },
{ name: 'Event 3', date: new Date('2023-03-05') }
];
// Sorting the array based on the 'date' property
data.sort((a, b) => a.date - b.date);
console.log(data);
Output
[ { name: 'Event 2', date: 2022-12-20T00:00:00.000Z }, { name: 'Event 1', date: 2023-01-15T00:00:00.000Z }, { name: 'Event 3', date: 2023-03-05T00:00:00.000Z } ]
2. Using sort() method with getTime() method
This method uses the sort
method but employs the getTime
method on Date
objects to obtain their numeric representations. Sorting is done based on these numeric values.
const data = [
{ name: 'Event 1', date: new Date('2023-01-15') },
{ name: 'Event 2', date: new Date('2022-12-20') },
{ name: 'Event 3', date: new Date('2023-03-05') }
];
// Sorting the array based on the 'date' property
data.sort((a, b) => a.date.getTime() - b.date.getTime());
console.log(data);
Output
[ { name: 'Event 2', date: 2022-12-20T00:00:00.000Z }, { name: 'Event 1', date: 2023-01-15T00:00:00.000Z }, { name: 'Event 3', date: 2023-03-05T00:00:00.000Z } ]
3. Using a custom sorting function
In this approach, a custom sorting function (sortByDate
) is defined, which compares the date
properties of the objects. This function is then used as an argument for the sort
method to achieve the desired sorting.
const data = [
{ name: 'Event 1', date: new Date('2023-01-15') },
{ name: 'Event 2', date: new Date('2022-12-20') },
{ name: 'Event 3', date: new Date('2023-03-05') }
];
// Custom sorting function
const sortByDate = (a, b) => {
return a.date - b.date;
};
// Sorting the array based on the 'date' property using the custom function
data.sort(sortByDate);
console.log(data);
Output
[ { name: 'Event 2', date: 2022-12-20T00:00:00.000Z }, { name: 'Event 1', date: 2023-01-15T00:00:00.000Z }, { name: 'Event 3', date: 2023-03-05T00:00:00.000Z } ]
4. Using Intl.DateTimeFormat and Array.prototype.sort()
To sort an object array by date using `Intl.DateTimeFormat` and `Array.prototype.sort()`, convert the date strings to formatted dates, then use the `sort` method with a custom compare function that compares the formatted dates.
const a = [
{ id: 1, date: '2022-01-15' },
{ id: 2, date: '2023-03-20' },
{ id: 3, date: '2021-09-10' }
];
a.sort((a, b) => new Intl.DateTimeFormat('en-US').format(new Date(a.date)) -
new Intl.DateTimeFormat('en-US').format(new Date(b.date)));
console.log(a);
Output
[ { id: 1, date: '2022-01-15' }, { id: 2, date: '2023-03-20' }, { id: 3, date: '2021-09-10' } ]