Object Array
Sorting Algorithms
Date Property
JavaScript
Programming Tips

How to sort an object array by date property?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Sorting an array of objects by a date property is straightforward in JavaScript once the date values are comparable. The real issues are usually not with sort() itself. They are with inconsistent date formats, accidental string comparisons, and repeated date parsing inside the comparator.

The Usual Case: ISO Date Strings

If the objects store ISO-like date strings, a basic comparator works:

javascript
1const events = [
2  { id: 1, name: "Meeting", date: "2023-09-12" },
3  { id: 2, name: "Webinar", date: "2023-07-16" },
4  { id: 3, name: "Conference", date: "2023-08-09" },
5];
6
7events.sort((a, b) => new Date(a.date) - new Date(b.date));
8
9console.log(events);

This sorts from earliest to latest because subtracting one Date from another compares their underlying timestamps.

Reverse the Comparator for Newest First

If you want descending order, swap the subtraction:

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

That gives most recent items first, which is a common UI requirement for activity feeds and logs.

Avoid Repeated Parsing for Large Arrays

If the array is large, repeatedly calling new Date(...) inside the comparator does unnecessary work. A cleaner pattern is to precompute timestamps.

javascript
1const events = [
2  { id: 1, name: "Meeting", date: "2023-09-12" },
3  { id: 2, name: "Webinar", date: "2023-07-16" },
4  { id: 3, name: "Conference", date: "2023-08-09" },
5].map(event => ({
6  ...event,
7  timestamp: new Date(event.date).getTime(),
8}));
9
10events.sort((a, b) => a.timestamp - b.timestamp);

This is especially useful if you sort repeatedly or if parsing is expensive relative to the array size.

Prefer Consistent Date Formats

The safest string format for JavaScript date parsing is ISO 8601 style input. Mixed or locale-specific formats are much more error-prone.

Good:

javascript
"2023-09-12"
"2023-09-12T15:30:00Z"

Riskier:

javascript
"09/12/2023"
"12-09-2023"

If your input format is inconsistent, normalize it before sorting. Otherwise you may get Invalid Date or different behavior across environments.

If You Already Have Date Objects, Compare Them Directly

When the date property is already a Date, the comparator becomes simpler:

javascript
1const events = [
2  { id: 1, date: new Date("2023-09-12") },
3  { id: 2, date: new Date("2023-07-16") },
4  { id: 3, date: new Date("2023-08-09") },
5];
6
7events.sort((a, b) => a.date - b.date);

That avoids reparsing entirely.

Be Careful with Time Zones

Two strings that look similar may represent different moments if one includes a time zone and the other does not. For example:

javascript
new Date("2023-09-12")
new Date("2023-09-12T00:00:00Z")

Depending on your application, you may need to decide whether you are sorting by:

  • absolute timestamp
  • local calendar date
  • UTC-normalized date

That decision matters for scheduling and reporting systems.

Common Pitfalls

  • Sorting date strings lexically when they are not in a safely sortable format.
  • Reparsing dates inside the comparator for large arrays without need.
  • Mixing local-time and UTC-style date strings without realizing it.
  • Ignoring Invalid Date values in dirty input data.
  • Assuming every human-readable date string parses consistently across environments.
  • Forgetting whether the UI wants oldest-first or newest-first ordering.

Summary

  • Use Array.prototype.sort() with a comparator that compares timestamps or Date objects.
  • ISO-style date strings are the safest input for predictable sorting.
  • Precompute timestamps if parsing cost matters.
  • Reverse the comparator for newest-first ordering.
  • Check format consistency and time-zone semantics before trusting the sorted output.
  • Validate a few representative records before relying on production sort behavior.
  • Small sample tests save debugging time later.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.