Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort array of Trello cards by date

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?

like image 597
sanjaypoyzer Avatar asked Jan 02 '26 00:01

sanjaypoyzer


1 Answers

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); }

like image 139
Vijay Raghunathan Avatar answered Jan 03 '26 13:01

Vijay Raghunathan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!