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

🌐
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....
Discussions

Dateオブジェクトの配列のソートについての初心...
Hello! I'm not sure if I got a brain freeze or my brain is just too rusty but I'm having a hard time sorting an Array of Date Objects according to… More on reddit.com
🌐 r/learnjavascript
How to sort an array of objects by 3 properties?
You're only handling the letter cases in your comparison function. You also need to handle string and counts (when not equal). Right now its returning the implicit undefined More on reddit.com
🌐 r/learnjavascript
15
1
January 31, 2017
[Javascript] sort an array with multiple keys, breaking ties of previous key with next key
You can pass a comparator to the array.sort method. A comparator is a function that looks at two elements and returns 0 if they're equal, a negative number if the first element should come first, and a positive number if the second element should come first. You could express your sort logic the long way like this: stuff.sort((x,y) => { if (x.number < y.number) { return 1; } else if (x.number > y.number) { return -1; } else { if (x.name < y.name) {return 1; } else if (x.name > y.name) {return -1; } else { return 0; } } }) Once you understand how this works, you might want to make the code more concise. stuff.sort((x,y) => { if (x.number === y.number) { return y.name.localeCompare(x.name); } else { return y.number-x.number; } }) Since we're on the internet, I predict that somebody will reply with a shorter implementation. More on reddit.com
🌐 r/CodingHelp
4
1
April 1, 2022
Sorting array of React Components

Your sort function returns (assuming your _id fields are positive) a positive number for every run. This is probably not what you want. Check out https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort.

Some other things you might want to consider in your code:

  1. Why do you pull state from the store with this.context.store.getState instead of connecting your component and using mapStateToProps (which would be more idiomatic)?

  2. Array.prototype.sort modified an array in place. This is almost certainly not what you want, since you're grabbing your array from the store.

note: To solve both the problems of mutating an object in place AND to get access to a more intuitive sort function, consider using lodash.

More on reddit.com
🌐 r/reactjs
9
3
April 5, 2016
🌐
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.
🌐
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.
🌐
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 ...
🌐
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 - To sort an array of objects by a date property, call sort() with a callback that subtracts the two dates.
🌐
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
🌐
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 - 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.
🌐
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.
🌐
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.
🌐
Byby
byby.dev › js-sort-by-date-value
How to sort an array by date value in JavaScript
May 15, 2023 - Sorting an array of objects by date property is a special case of sorting an array by object property.
🌐
Codemia
codemia.io › home › knowledge hub › how to sort an object array by date property?
How to sort an object array by date property? | Codemia
January 2, 2025 - Use Array.prototype.sort() with a comparator that compares timestamps or Date objects.
🌐
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 - In the above example, we have an array called events with three objects, each having an eventName and a date. JavaScript offers a built-in sort() method for arrays. By default, it sorts the elements as strings.
🌐
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 - Often, dates are part of objects in an array, such as events or posts. To sort these arrays by date, use the sort method with a comparison function that extracts and compares the date properties.
🌐
xjavascript
xjavascript.com › blog › how-to-sort-an-object-array-by-date-property
How to Sort a JavaScript Object Array by Date Property (Newest First) Using sort() and Custom Comparator — xjavascript.com
Sorting a JavaScript object array by date (newest first) requires: A custom comparator function for sort(). Converting date strings to Date objects for reliable comparison. Subtracting the older date from the newer date (bDate - aDate) to order ...
🌐
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 - Day.js is a minimalist JavaScript library that parses, validates, manipulates, and displays dates. The dayjs().diff() method can be used to sort arrays of objects by date values efficiently.
🌐
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...
🌐
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
Author: bobbyhadz