-1

I want to sort an array of objects by date, but the problem is their date is in this format - 2014-07-17T13:49:12.767Z.

Here an example of one object in this array

{
  id: 578,
  creationDate: "2014-07-16T20:56:04.710Z",
  creationUser: "FCOUT",
  modificationDate: "2014-07-17T13:49:12.767Z",
  modificationUser: "FCOUT",
  name: "Regra Filipe",
  description: "Teste",
  type: "Message",
  regulation: null,
  structure: 1,
  deleted: false,
}

I have to sort them by modification date or creation date!

DontVoteMeDown
  • 19,660
  • 10
  • 65
  • 96
  • 1
    Why is it a problem that the dates are in UTC format? Doesn't that make things easier? Just use a normal sort: http://www.w3schools.com/jsref/jsref_sort.asp – Søren Ullidtz Jul 17 '14 at 18:37
  • 1
    What exactly makes you think that the date format is a problem? Did you even try anything? – Patrick Q Jul 17 '14 at 18:38
  • You can find some useful answers to this topic here: **[Sort Javascript Object Array By Date](http://stackoverflow.com/a/26759127/2247494)** – jherax Nov 05 '14 at 14:12

2 Answers2

2

Just write a sort function as given here. In your case the compare function will just be comparing two strings , which in the date format you have mentioned should work out of the box.

ie you can compare the two dates as strings.

Community
  • 1
  • 1
smk
  • 3,901
  • 3
  • 23
  • 36
1

Mozilla sorting documentation

use the compare function

function(a, b) {
    if (a.creationDate < b.creationDate) { return -1; }
    if (a.creationDate > b.creationDate) { return 1; }
    return 0;
}

Your date format allows the creationDate strings to just be compared lexicographically

Strikeskids
  • 3,602
  • 9
  • 25