If your objects have the date information within a String field:

yourArray.sort(function(a, b) { return new Date(a.date) - new Date(b.date) })

or, if they have it within a Date field:

yourArray.sort(function(a, b) { return a.date - b.date })
Answer from turdus-merula on Stack Overflow
๐ŸŒ
Nesin
nesin.io โ€บ blog โ€บ sorting-dates-javascript
Sorting Dates in Javascript using Array.sort method
March 9, 2023 - const input = [ {"date":"2022-12-07"}, ... ] const ascendingOrder = input.sort((a, b) => new Date(a.date) - new Date(b.date)); const descendingOrder = input.sort((a, b) => new Date(b.date) - new Date(a.date)); Sort method takes in a compare function which has params (first and second item)....
๐ŸŒ
Medium
medium.com โ€บ @ryan_forrester_ โ€บ sorting-by-date-in-javascript-how-to-guide-2c6d2d0d230b
Sorting by Date in JavaScript (How to Guide) | by ryan | Medium
September 12, 2024 - To sort dates in ascending order, compare date objects using their time values. const dates = [ new Date('2023-07-05'), new Date('2021-01-15'), new Date('2022-12-25') ]; dates.sort((a, b) => a - b); console.log(dates); // Output: [ Date ...
Top answer
1 of 16
2235

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
387

@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

๐ŸŒ
Medium
medium.com โ€บ @danialashrafk20sw037 โ€บ sorting-dates-in-javascript-89c63e143acf
Sorting Dates in JavaScript. Introduction to Sorting Dates inโ€ฆ | by Danialashrafksw | Medium
March 8, 2024 - In the example above, the `sort()` method sorts the array of date objects in ascending order by default. However, for dates, subtracting one date from another returns the difference in milliseconds, allowing for accurate date comparison.
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ javascript sort date ascending and descending | example code
JavaScript sort date ascending and descending | Example code
January 31, 2023 - <!DOCTYPE html> <html> <body> <script> var array = ["25-Jul-2017 12:46:39 pm", "02-Jul-2017 12:52:23 pm", "01-Jul-2021 12:47:18 pm", "25-Jul-2017 12:59:18 pm"]; array.sort((a, b) => new Date(b).getTime() - new Date(a).getTime()) console.log(array) </script> </body> </html> ... <script> var a = [ { "name": "February", "date": "2018-02-04T17:00:00.000Z", }, { "name": "March", "date": "2018-03-04T17:00:00.000Z", }, { "name": "January", "date": "2018-01-17T17:00:00.000Z", } ] a.sort(function(a,b){ return new Date(a.date) - new Date(b.date) }) console.log(a) </script>
๐ŸŒ
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 - The desired output will be an array sorted by dates in ascending order: ... This happens because the sort() method works by sorting the string representations of elements, not the actual elements. For instance, D from 'Dec 31 2000' is lexicographically lesser than J from 'Jan 22 2021' and so on.
๐ŸŒ
Talkerscode
talkerscode.com โ€บ howto โ€บ javascript-sort-date-ascending-and-descending.php
JavaScript Sort Date Ascending And Descending
In first sorting โ€˜t1โ€™ had descending order result which is by those two parameter subtraction operation, here we subtracted second parameter with first parameter. Itโ€™s reverse process we gets ascending order result. <!DOCTYPE html> <html> <head> <title>DATE SORT</title> </head> <body> <script> var t1,t2=[]; var arr1 = [ "01-Sep-2019 12:50:20 pm","04-Jan-2022 12:22:50 pm","10-Oct-2009 12:12:03 pm" ]; var arr2 = [ "01-Sep-2019 12:50:20 pm","04-Jan-2022 12:22:50 pm","10-Oct-2009 12:12:03 pm" ]; t1=arr1.sort((a,b)=> new Date(b).getTime() - new Date(a).getTime()) t2=arr2.sort((a,b)=> new Date(a).getTime() - new Date(b).getTime()) console.log(t1); console.log(t2); </script> </body> </html>
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ sort-an-object-array-by-date-in-javascript
Sort an Object Array by Date in JavaScript - GeeksforGeeks
May 24, 2025 - Below are the approaches to sort ... sort method along with Date objects. The sort method compares Date objects directly, sorting the array in ascending order based on the date property....
๐ŸŒ
Byby
byby.dev โ€บ js-sort-by-date-value
How to sort an array by date value in JavaScript
Here is an example of using date-fns ... 2012 08:00:00 AM"} ]; // Sort by date value in descending order array.sort(function(a, b) { // Use compareDesc function from date-fns library // It takes two date values and returns a number that indicates their order return compareDesc(a.date, ...
๐ŸŒ
MSR
rajamsr.com โ€บ home โ€บ learn javascript sort by date with these 11 best examples
Learn JavaScript Sort by Date with These 11 Best Examples | MSR - Web Dev Simplified
December 2, 2023 - To sort the JavaScript Dates in descending order, we can use the reverse() method after sorting the array in ascending order. const dates = ["2023-07-09", "2022-10-15", "2024-02-03", "2023-01-01"]; dates.sort((a, b) => new Date(a) - new ...
๐ŸŒ
Code Highlights
code-hl.com โ€บ home โ€บ javascript โ€บ tutorials
How to JavaScript Sort by Date Easily | Code Highlights
June 12, 2024 - In this tutorial, we've covered how to sort dates in JavaScript using the sort() method and the Date object. We've seen how to sort dates in ascending and descending order, handle different date formats, and sort dates within objects.
๐ŸŒ
Atomizedobjects
atomizedobjects.com โ€บ blog โ€บ javascript โ€บ how-to-sort-an-array-by-date-in-javascript
How to sort an array by date in JavaScript | Atomized Objects
In this post find out about how to sort an array by date in JavaScript as well as sorting in descending order, and by timestamps.
๐ŸŒ
GitHub
gist.github.com โ€บ onpubcom โ€บ 1772996
How to Sort an Array of Dates with JavaScript ยท GitHub
I think @obonyojimmy 's functions actually sort in descending order. Switch to dateArray.sort((a,b)=> a-b) for ascending.
๐ŸŒ
Pipinghot
pipinghot.dev โ€บ snippet โ€บ array-sort-date-javascript
Sort an array by date in JavaScript
September 29, 2025 - This will sort the dates into ascending order. To sort in descending order, replace return new Date(a) - new Date(b) with return new Date(b) - new Date(a). Sometimes, however, you may want to keep the original array.
๐ŸŒ
Dana Woodman
danawoodman.com โ€บ writing โ€บ sort-javascript-array-by-date-javascript-typescript
How to sort an array by date value in JavaScript and TypeScript?
January 31, 2022 - const descending = [...users].sort( (a, b) => b.joined.getTime() - a.joined.getTime() ); Get oldest user signups first (ascending order): const ascending = [...users].sort( (a, b) => a.joined.getTime() - b.joined.getTime() ); Note here we are using Array Destructuring to create a new copy of the array when weโ€™re doing the sort.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-sort-date-array-in-javascript
How to sort date array in JavaScript
const arr = [ [ '02/13/2015', 0.096 ], [ '11/15/2013', 0.189 ], [ '05/15/2014', 0.11 ], [ '12/13/2013', 0.1285 ], [ '01/15/2013', 0.12 ], [ '01/15/2014', 0.11 ], [ '02/14/2014', 0.11 ], [ '03/14/2014', 0.11 ], [ '01/15/2015', 0.096 ], [ '07/15/2015', 0.096 ], [ '04/15/2013', 0.12 ], [ '04/15/2014', 0.11 ], [ '05/15/2013', 0.12 ], [ '06/14/2013', 0.12 ], [ '06/16/2014', 0.11 ], [ '07/15/2013', 0.12 ], [ '07/15/2014', 0.11 ], [ '03/16/2015', 0.096 ] ]; const sortByDate = arr => { const sorter = (a, b) => { return new Date(a[0]) - new Date(b[0]); }; arr.sort(sorter); }; sortByDate(arr); console.log(arr);
๐ŸŒ
Mastering JS
masteringjs.io โ€บ tutorials โ€บ fundamentals โ€บ sort-by-date
How to Sort an Array by Date in JavaScript - Mastering JS
Similarly, sorting an array of objects by a date property is easy. Just subtract the two date properties in the sort() callback.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ javascript โ€บ sort by date javascript
How to Sort by Date in JavaScript | Delft Stack
March 11, 2025 - In this example, the same array of dates is sorted, but this time in descending order. By reversing the subtraction in the comparison function, we get the dates sorted from the most recent to the oldest.
๐ŸŒ
GoLinuxCloud
golinuxcloud.com โ€บ home โ€บ javascript โ€บ how to sort by date in javascript? [solved]
How to sort by date in JavaScript? [SOLVED] | GoLinuxCloud
November 11, 2022 - All dates are arranged in ascending order. To achieve this, we have passed an anonymous function that compares each date by subtracting both dates (the previous value from the current value) from each other to see which values will be higher ...