Simplest Answer

array.sort(function(a,b){
  // Turn your strings into dates, and then subtract them
  // to get a value that is either negative, positive, or zero.
  return new Date(b.date) - new Date(a.date);
});

More Generic Answer

array.sort(function(o1,o2){
  if (sort_o1_before_o2)    return -1;
  else if(sort_o1_after_o2) return  1;
  else                      return  0;
});

Or more tersely:

array.sort(function(o1,o2){
  return sort_o1_before_o2 ? -1 : sort_o1_after_o2 ? 1 : 0;
});

Generic, Powerful Answer

Define a custom non-enumerable sortBy function using a Schwartzian transform on all arrays :

(function(){
  if (typeof Object.defineProperty === 'function'){
    try{Object.defineProperty(Array.prototype,'sortBy',{value:sb}); }catch(e){}
  }
  if (!Array.prototype.sortBy) Array.prototype.sortBy = sb;

  function sb(f){
    for (var i=this.length;i;){
      var o = this[--i];
      this[i] = [].concat(f.call(o,o,i),o);
    }
    this.sort(function(a,b){
      for (var i=0,len=a.length;i<len;++i){
        if (a[i]!=b[i]) return a[i]<b[i]?-1:1;
      }
      return 0;
    });
    for (var i=this.length;i;){
      this[--i]=this[i][this[i].length-1];
    }
    return this;
  }
})();

Use it like so:

array.sortBy(function(o){ return o.date });

If your date is not directly comparable, make a comparable date out of it, e.g.

array.sortBy(function(o){ return new Date( o.date ) });

You can also use this to sort by multiple criteria if you return an array of values:

// Sort by date, then score (reversed), then name
array.sortBy(function(o){ return [ o.date, -o.score, o.name ] };

See http://phrogz.net/JS/Array.prototype.sortBy.js for more details.

Answer from Phrogz on Stack Overflow
Top answer
1 of 16
2236

Simplest Answer

array.sort(function(a,b){
  // Turn your strings into dates, and then subtract them
  // to get a value that is either negative, positive, or zero.
  return new Date(b.date) - new Date(a.date);
});

More Generic Answer

array.sort(function(o1,o2){
  if (sort_o1_before_o2)    return -1;
  else if(sort_o1_after_o2) return  1;
  else                      return  0;
});

Or more tersely:

array.sort(function(o1,o2){
  return sort_o1_before_o2 ? -1 : sort_o1_after_o2 ? 1 : 0;
});

Generic, Powerful Answer

Define a custom non-enumerable sortBy function using a Schwartzian transform on all arrays :

(function(){
  if (typeof Object.defineProperty === 'function'){
    try{Object.defineProperty(Array.prototype,'sortBy',{value:sb}); }catch(e){}
  }
  if (!Array.prototype.sortBy) Array.prototype.sortBy = sb;

  function sb(f){
    for (var i=this.length;i;){
      var o = this[--i];
      this[i] = [].concat(f.call(o,o,i),o);
    }
    this.sort(function(a,b){
      for (var i=0,len=a.length;i<len;++i){
        if (a[i]!=b[i]) return a[i]<b[i]?-1:1;
      }
      return 0;
    });
    for (var i=this.length;i;){
      this[--i]=this[i][this[i].length-1];
    }
    return this;
  }
})();

Use it like so:

array.sortBy(function(o){ return o.date });

If your date is not directly comparable, make a comparable date out of it, e.g.

array.sortBy(function(o){ return new Date( o.date ) });

You can also use this to sort by multiple criteria if you return an array of values:

// Sort by date, then score (reversed), then name
array.sortBy(function(o){ return [ o.date, -o.score, o.name ] };

See http://phrogz.net/JS/Array.prototype.sortBy.js for more details.

2 of 16
388

@Phrogz answers are both great, but here is a great, more concise answer:

array.sort(function(a,b) { return a.getTime() - b.getTime() });

Using the arrow function way

array.sort((a,b) => a.getTime() - b.getTime());

found here: Sort date in Javascript

🌐
Flavio Copes
flaviocopes.com › home › javascript › how to sort an array by date value in javascript
How to sort an array by date value in JavaScript - Flavio Copes
September 21, 2026 - Learn how to sort a JavaScript array of objects by a date property with sort() and a callback that subtracts the dates, or toSorted() to keep the original.
Discussions

How can I sort an array of object with date (created_at) from the latest to the oldest .Here is an example bellow of an array I want to sort
You should check usort: https://www.php.net/manual/en/function.usort.php More on reddit.com
🌐 r/PHPhelp
10
2
May 25, 2022
Sort array of arrays by date in the array?
What I know is that what helps is the fact that with the arrays the “string” is separated by a comma. So you could create two variables based on the arguments and split them by the comma. Then you can run the array.sort on both variables and return the value. Tbf I’m quite new too but this actually looks interesting! I will give this an attempt and see if I can get it working too! More on reddit.com
🌐 r/learnjavascript
11
1
April 17, 2021
🌐
Favtutor
favtutor.com › articles › sort-by-date-javascript
Sort an Object Array by Date in JavaScript (with code)
February 26, 2024 - Let's apply this in our example: // Convert date strings to Date objects and then sort the array employeesData.sort((a, b) => new Date(a.joinDate) - new Date(b.joinDate)); // Display the sorted array based on the 'joinDate' property ...
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › sort-by-date
How to Sort an Array by Date in JavaScript - Mastering JS
October 14, 2021 - Similarly, sorting an array of objects by a date property is easy. Just subtract the two date properties in the sort() callback.
🌐
DEV Community
dev.to › askyt › sorting-an-array-of-objects-by-date-in-javascript-3pnb
Sorting an Array of Objects by Date in JavaScript - DEV Community
January 7, 2025 - The simplest approach to sort by date is leveraging JavaScript’s built-in Date object.
🌐
Medium
medium.com › @tariibaba › javascript-sort-array-by-date-a21ca45e2605
How to Sort an Object Array by Date in JavaScript | Medium
May 26, 2022 - This means that we can just as easily sort the array in descending order like this: events.sort((a, b) => b.date.getTime() - a.date.getTime()); Now the result will be positive when b is larger than a and this will make sort() place b first.
Find elsewhere
🌐
JavaScript in Plain English
javascript.plainenglish.io › javascript-how-to-sort-an-array-of-objects-by-date-8ca1d47dc695
JavaScript: How to Sort an Array of Objects by Date | by Alex Zelinsky | JavaScript in Plain English
September 26, 2023 - Typically, we’re working with ... events with three objects, each having an eventName and a date. JavaScript offers a built-in sort() method for arrays....
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-sort-array-of-objects-by-date-property
Sort an Array of Objects by Date property in JavaScript | bobbyhadz
March 6, 2024 - Call the sort() method on the array. Subtract the date in the second object from the date in the first.
🌐
Sling Academy
slingacademy.com › article › javascript-sorting-an-array-of-objects-by-date-property
JavaScript: Sorting an Array of Objects by Date Property - Sling Academy
March 7, 2023 - 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);
🌐
Coding Beauty
codingbeautydev.com › home › posts › how to sort an object array by date in javascript
How to Sort an Object Array By Date in JavaScript - Coding Beauty
November 14, 2022 - This means that we can just as easily sort the array in descending order like this: events.sort((a, b) => b.date.getTime() - a.date.getTime()); Now the result will be positive when b is larger than a and this will make sort() place b first.
🌐
YouTube
youtube.com › watch
Date Sorting On An Array Of Objects | JavaScript - YouTube
In this video we will see how to perform date sorting on an array of objects.It includes string sorting as well. This is pure javascript logic which can be u...
Published: September 26, 2022
🌐
Byby
byby.dev › js-sort-by-date-value
How to sort an array by date value in JavaScript
May 15, 2023 - The compareDesc function from date-fns library is a convenient way to compare two date values. It returns a negative number if the first date is after the second date, a positive number if the first date is before the second date, and zero if the dates are equal. You can use it as a compare function for the sort() method of the array object.
🌐
Stack Abuse
stackabuse.com › how-to-sort-an-array-by-date-in-javascript
How to Sort an Array by Date in JavaScript
December 28, 2021 - Note: In this guide, we'll be sorting an array of dates, but the same rules apply in the case of sorting an array of objects containing a date property · As previously mentioned, the Array data structure in JavaScript has a built-in sort() method used to sort the elements of an array.
🌐
Squash
squash.io › how-to-sort-a-javascript-object-array-by-date-property
How to Sort a Javascript Object Array by a Date Property
November 26, 2023 - To sort a JavaScript object array by a date property, you can use the Array.prototype.sort() method along with a custom comparison function.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › sort-array-of-objects-by-single-key-with-date-value-in-javascript
JavaScript - Sort JS Arrays of Objects by Date Key - GeeksforGeeks
July 23, 2025 - ... // Import moment.js const moment ... { name: 'G4G', date: '2021-03-20' } ]; // Sort array by date using moment.js arr.sort((a, b) => moment(a.date).diff(moment(b.date))); console.log(arr);...
🌐
GeeksforGeeks
geeksforgeeks.org › sort-array-of-objects-by-single-key-with-date-value-in-javascript
JavaScript – Sort JS Arrays of Objects by Date Key | GeeksforGeeks
November 13, 2024 - There are two methods to solve this problem which are discussed below: Approach 1:First compare the first property, if both are unequal then sort accordingly.If they are equal then do the same ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › sort-an-object-array-by-date-in-javascript
Sort an Object Array by Date in JavaScript - GeeksforGeeks
May 24, 2025 - const data = [ { name: 'Event 1', ... property data.sort((a, b) => a.date.getTime() - b.date.getTime()); console.log(data); ... [ { 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 } ] In this approach, a custom sorting function (sortByDate) is defined, which compares the date properties of the objects...