Sort an Object Array by Date in JavaScript
Last Updated :
24 May, 2025
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.
JavaScript
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.
JavaScript
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.
JavaScript
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 }
]
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.
JavaScript
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' }
]
Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
CPU Scheduling in Operating Systems CPU scheduling is a process used by the operating system to decide which task or process gets to use the CPU at a particular time. This is important because a CPU can only handle one task at a time, but there are usually many tasks that need to be processed. The following are different purposes of a
8 min read
Introduction to Tree Data Structure Tree data structure is a hierarchical structure that is used to represent and organize data in the form of parent child relationship. The following are some real world situations which are naturally a tree.Folder structure in an operating system.Tag structure in an HTML (root tag the as html tag) or
15+ min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
What Is Cloud Computing ? Types, Architecture, Examples and Benefits Nowadays, Cloud computing is adopted by every company, whether it is an MNC or a startup many are still migrating towards it because of the cost-cutting, lesser maintenance, and the increased capacity of the data with the help of servers maintained by the cloud providers. Cloud Computing means stori
15 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read