To sort an array of objects by date property in JavaScript, you can use the built-in Array.sort() method. This method takes an optional compare function as an argument that can be used to customise the sort order.
The compare function should compare the date properties of two objects in the array and return a value indicating how the two objects should be ordered.
Sort An Array Of Objects By Date Property In JavaScript
For example, consider the following array of objects:
var arr = [
{
id: 1,
name: ‘John’,
date: new Date(2018, 1, 1)
},
{
id: 2,
name: ‘Jane’,
date: new Date(2017, 10, 15)
},
{
id: 3,
name: ‘Joe’,
date: new Date(2017, 5, 20)
}
];
To sort this array of objects by date property in ascending order –
var sorted = arr.sort((obj1, obj2) => Number(obj1.date) – Number(obj2.date));
console.log(sorted);
Output:
[{
date: [object Date] { … },
id: 3,
name: “Joe”
}, {
date: [object Date] { … },
id: 2,
name: “Jane”
}, {
date: [object Date] { … },
id: 1,
name: “John”
}]
To sort this array of objects by date property in descending order –
var sorted = arr.sort((obj1, obj2) => Number(obj2.date) – Number(obj1.date));
console.log(sorted);
Output:
[{
date: [object Date] { … },
id: 1,
name: “John”
}, {
date: [object Date] { … },
id: 2,
name: “Jane”
}, {
date: [object Date] { … },
id: 3,
name: “Joe”
}]
Here, we have assumed that the date is a date object. If we have date strings in the object, we can use the map() function to convert the date string to a date object.
For example, –
var arr = [{
id: 1,
name: ‘John’,
date: ‘2018-01-01’
},
{
id: 2,
name: ‘Jane’,
date: ‘2017-10-15’
},
{
id: 3,
name: ‘Joe’,
date: ‘2017-05-20’
}
];
To sort this array of objects by date property in ascending order –
var arr1 = arr.map(item => ({...item, date: new Date(item.date)}));
var sorted = arr1.sort((obj1, obj2) => Number(obj1.date) – Number(obj2.date));
console.log(sorted);
Output:
[{
date: [object Date] { … },
id: 3,
name: “Joe”
}, {
date: [object Date] { … },
id: 2,
name: “Jane”
}, {
date: [object Date] { … },
id: 1,
name: “John”
}]
To sort this array of objects by date property in descending order –
var arr1 = arr.map(item => ({...item, date: new Date(item.date)}));
var sorted = arr1.sort((obj1, obj2) => Number(obj2.date) – Number(obj1.date));
console.log(sorted);
Output:
[{
date: [object Date] { … },
id: 1,
name: “John”
}, {
date: [object Date] { … },
id: 2,
name: “Jane”
}, {
date: [object Date] { … },
id: 3,
name: “Joe”
}]
Sort An Array Of Objects By Date Property In JavaScript
Sorting an array of objects by date property is a common task that you might need to do in your applications. And using the built-in Array.sort() method with a custom compare function is an easy and straightforward way to do it.
This technique can be used to sort arrays of objects by any property – not just date. So if you need to sort an array of objects by name, or by id, or by any other property, you can use the same technique. Just make sure to write a compare function that is appropriate for the property you want to sort by.