3

I'm getting cards assigned to the user using the Trello API. It's an array called cards. Each item in the array has another array nested inside it, with the properties of each card inside it.

So I have my cards array, and then each Object inside it has it's own array.

E.g:

[Object, Object, Object]
    0: Object
        due: 2013-11-29T12:00:00.000Z
    1: Object
        due: 2013-11-26T12:00:00.000Z
    2: Object
        due: 2013-12-28T12:00:00.000Z

I want to sort my cards by the due property of the cards.

I get this array like this:

    Trello.get("members/me/cards", function(cards) {
        console.log(cards);
    }); 

And I can get each due property with console.log(dates[1].due)

So my question is, how can I order these Objects by this datetime?

suryanaga
  • 3,728
  • 11
  • 36
  • 47
  • 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 15:43

1 Answers1

2

All JS arrays have a built in sort function. cards.sort(function(a,b) { a1 = new Date(a.due); b1 = new Date(b.due); return a1<b1? -1: a1 > b ? 1 : 0); }

  • Yes. Additionally, if the values are all at UTC (they all have a `Z` on them) you can omit creating a `Date` object and just sort them as strings, since they are then lexicographically sortable. – Matt Johnson-Pint Jan 02 '14 at 21:26
  • 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 15:43