Sling Academy
Home/JavaScript/JavaScript: Sorting an Array of Objects by Date Property

JavaScript: Sorting an Array of Objects by Date Property

Last updated: March 07, 2023

To sort an array of objects by date property in JavaScript, we can use the Array.prototype.sort() method with a custom comparator function. The comparator function should compare the date values of the objects using the Date constructor and return a value based on the comparison.

When you want to sort in ascending date order, the comparator function will look like this:

((a, b) => new Date(a.dateProperty) - new Date(b.dateProperty)

When you want to sort in descending date order:

((a, b) => new Date(b.dateProperty) - new Date(a.dateProperty)

Examples

Sort users by account age, from newest to oldest

The code:

const users = [
  { id: 1, name: 'Sasuke Honda', joinedDateTime: '2022-01-01T12:30:00.000Z' },
  {
    id: 2,
    name: 'Ranni The Witch',
    joinedDateTime: '2021-12-31T15:45:00.000Z',
  },
  { id: 3, name: 'Wolf', joinedDateTime: '2022-02-01T09:00:00.000Z' },
  { id: 4, name: 'Sling Academy', joinedDateTime: '2024-02-01T09:00:00.000Z' },
];

users.sort((a, b) => new Date(b.joinedDateTime) - new Date(a.joinedDateTime));

console.log(users);

Output:

[
  {
    id: 4,
    name: 'Sling Academy',
    joinedDateTime: '2024-02-01T09:00:00.000Z'
  },
  { id: 3, name: 'Wolf', joinedDateTime: '2022-02-01T09:00:00.000Z' },
  {
    id: 1,
    name: 'Sasuke Honda',
    joinedDateTime: '2022-01-01T12:30:00.000Z'
  },
  {
    id: 2,
    name: 'Ranni The Witch',
    joinedDateTime: '2021-12-31T15:45:00.000Z'
  }
]

Note that the sort() method modifies the original array in place. If you want to keep the original array intact, just clone it. See this article if you aren’t familiar with this task.

Sort books by publication date, from oldest to newest

The code:

const books = [
  { title: 'The Great Gatsby', publishDate: '1925-04-10' },
  { title: 'To Kill a Mockingbird', publishDate: '1960-07-11' },
  { title: '1984', publishDate: '1949-06-08' },
];

books.sort((a, b) => new Date(a.publishDate) - new Date(b.publishDate));

console.log(books);

Output:

[
  { title: 'The Great Gatsby', publishDate: '1925-04-10' },
  { title: '1984', publishDate: '1949-06-08' },
  { title: 'To Kill a Mockingbird', publishDate: '1960-07-11' }
]

That’s it. Happy coding!

    Next Article: JavaScript: Get an array of dates between 2 given dates

    Previous Article: JavaScript: 2 Ways to Clone (Deep Copy) a Date Object

    Series: Date and Time in JavaScript: Basic to Advanced Tutorials

    JavaScript

    You May Also Like

  • Handle Zoom and Scroll with the Visual Viewport API in JavaScript
  • Improve Security Posture Using JavaScript Trusted Types
  • Allow Seamless Device Switching Using JavaScript Remote Playback
  • Update Content Proactively with the JavaScript Push API
  • Simplify Tooltip and Dropdown Creation via JavaScript Popover API
  • Improve User Experience Through Performance Metrics in JavaScript
  • Coordinate Workers Using Channel Messaging in JavaScript
  • Exchange Data Between Iframes Using Channel Messaging in JavaScript
  • Manipulating Time Zones in JavaScript Without Libraries
  • Solving Simple Algebraic Equations Using JavaScript Math Functions
  • Emulating Traditional OOP Constructs with JavaScript Classes
  • Smoothing Out User Flows: Focus Management Techniques in JavaScript
  • Creating Dynamic Timers and Counters with JavaScript
  • Implement Old-School Data Fetching Using JavaScript XMLHttpRequest
  • Load Dynamic Content Without Reloading via XMLHttpRequest in JavaScript
  • Manage Error Handling and Timeouts Using XMLHttpRequest in JavaScript
  • Handle XML and JSON Responses via JavaScript XMLHttpRequest
  • Make AJAX Requests with XMLHttpRequest in JavaScript
  • Customize Subtitle Styling Using JavaScript WebVTT Integration