0

I got an array of objects which I want to sort by date. I've got the date formatted like this with a property - created_at: "2014-09-26 19:27:43".

If any one could point me in the right direction, that would be great.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
user3355603
  • 1,091
  • 2
  • 12
  • 19
  • 1
    [Have you read up on how the Array `.sort()` function works?](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) – Pointy Dec 29 '14 at 16:40

1 Answers1

1

Use Date objects for comparison and Array.prototype.sort() to sort.

var array = ["2014-09-26 19:27:43","2014-09-26 19:27:42","2014-09-23 19:27:43"];
var sortedArray = array.sort(function(a,b){
       return new Date(a) - new Date(b);
    });
document.write(sortedArray);
Sampath Liyanage
  • 4,776
  • 2
  • 28
  • 40