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
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

๐ŸŒ
SPGuides
spguides.com โ€บ typescript-sort-array-of-objects-by-date-descending
TypeScript Sort Array Of Objects By Date Descending
June 29, 2025 - Define your array with date properties, and then apply the sort method with a custom comparator function: array.sort((a, b) => b.date.getTime() โ€“ a.date.getTime());. This approach effectively sorts your array objects based on the date property ...
๐ŸŒ
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() ); ... Note here we are using Array Destructuring to create a new copy of the array when weโ€™re doing the sort. The reason for this is that sort mutates (or changes in-place) our original array as well as ...
๐ŸŒ
SPGuides
spguides.com โ€บ typescript-sort-array
TypeScript Sort By Date | TypeScript Sort Array By Date
June 29, 2025 - This is an example of a Typescript sort array by date. Here, we will see how to sort of objects by date descending using sort() method in typescript.
๐ŸŒ
Reddit
reddit.com โ€บ r/typescript โ€บ function to sort by some date attribute
r/typescript on Reddit: Function to sort by some date attribute
July 25, 2023 -

Hey community,

I want to write a re-usable function byDate that creates a comparator that I can pass to a sort function like this:

const obj1 = {...somedata, startedAt: new Date()}

...

[obj1, obj2, obj3].sort(byDate('startedAt'))

I do have a implementation that looks like this:

export type DateKeys<T> = {

  [K in keyof T]: T[K] extends Date | undefined ? K : never

}[keyof T]



export function byDate<T>(d: DateKeys<T>) {

  return (a: T, b: T) => a[d].getTime() - b[d].getTime()

}

Unfortunately a[d] is types as T[DateKeys<T>] where I hopped it would be Date.

I have the feeling, that there must be a simple solution for this.

๐ŸŒ
GitHub
gist.github.com โ€บ onildoaguiar โ€บ 6cf7dbf9e0f0b8684eb5b0977b40c5ad
Sorting an array by date with Moment.js ยท GitHub
const sortedArray = _.orderBy(array, (o: any) => { return moment(o.date.format('YYYYMMDD'); }, ['asc']);
๐ŸŒ
Linux Hint
linuxhint.com โ€บ sort-an-array-of-objects-by-date-property-in-javascript
Sort an Array of Objects by Date Property in JavaScript
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-sort-array-of-objects-by-date-property
Sort an Array of Objects by Date property in JavaScript | bobbyhadz
To sort the array of objects by date property in descending order, you just have to subtract the timestamp of the first date from the timestamp of the second.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ sort-an-object-array-by-date-in-javascript
Sort an Object Array by Date in JavaScript | GeeksforGeeks
May 24, 2025 - This needs to be done by converting the date representation into the proper comparable formats and then applying the sorting function to sort the array in ascending or descending order.
๐ŸŒ
Pipinghot
pipinghot.dev โ€บ sort-an-array-by-date-in-typescript
Sort an array by date in TypeScript
September 29, 2025 - const arrayOfDates: string[] = ... into ascending order. To sort in descending order, replace return new Date(a) - new Date(b) with return new Date(b) - new Date(a)....
๐ŸŒ
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, ... Date 2023-07-05T00:00:00.000Z ] To sort dates in descending order, reverse the comparison in the sort method....
๐ŸŒ
Byby
byby.dev โ€บ js-sort-by-date-value
How to sort an array by date value in JavaScript
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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
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...
๐ŸŒ
GitHub
gist.github.com โ€บ onpubcom โ€บ 1772996
How to Sort an Array of Dates with JavaScript ยท GitHub
This means that a < or > comparison between two dates calls the Date.prototype.valueOf() function for each date object before comparing the values, giving an accurate, absolute comparison. ... probably late but also a simple subtraction of data prototypes to sort function would work just fine , reduce unnecessary return -1 like : var date_sort_asc = function (date1, date2) { // Accending order. return date2 - date1 }; even better es6 one-liner: dataArray.sort((a,b)=> b-a) ... I think @obonyojimmy 's functions actually sort in descending order.
๐ŸŒ
SPGuides
spguides.com โ€บ sort-array-of-objects-in-typescript
How to Sort Arrays of Objects in TypeScript
June 23, 2025 - You can see the exact output in the screenshot below: For more complex comparisons, you can easily extend this pattern. For example, if you want to sort in descending order, just reverse the comparison: