Trello API Sorting Cards in List
Asked by Billy Stidham:

Using the REST API, is there a best method for sorting cards in a list by due date?

I imagine I'd need to pull the cards that are all of the lists that I want to sort, figure out their positions vs their new positions, and generate a set of card pos update actions for cards that need an update, and then send those to the server by card PUT call(s).

Wondering if there's a better way. Can't seem to find anything in the API documentation (such as a lists-\>sort command) or by duckduckgo or the community search.

Thanks.


Answers:
author: Jose Emanuel Delgado
body: Hi, i was searching for the same method, but finally i did it by my own.

Here is the code to sort by year and description, feel free to adapted and use it.

**function compare( a, b ) {**

**const nameA = a.description.toUpperCase();**

**const nameB = b.description.toUpperCase();**

<br />

**if ( a.year \< b.year ){**

**return -1; }**

**if ( a.year \> b.year ){**

**return 1; }**

**if ( a.year === b.year){**

**if(nameA.localeCompare(nameB) \< 0) {**

**return -1; }**

**if(nameA.localeCompare(nameB) \> 0){**

**return 1; }**

**}**

**return 0;**

**}**

**disc.album.sort( compare );**

*Then you can do the POST to the API with the sorted Array of objects.*


author: Iain Dooley
body: [@Billy Stidham](/t5/user/viewprofilepage/user-id/3578634) you can see the list sorting algorithm I use in Trellinator here:

<https://www.theprocedurepeople.com/trellinator-automate-trello/docs/trellinator-libs_List.js.html#line120>

The docs for it are here:

<https://www.theprocedurepeople.com/trellinator-automate-trello/docs/module-TrelloEntities.List.html>

You can read more about Trellinator here:

<https://community.atlassian.com/t5/Marketplace-Apps-Integrations/Introducing-Trellinator-Automate-Trello-with-Google-Apps-Script/ba-p/925271>

comments: author: Billy Stidham
body: [@Iain Dooley](/t5/user/viewprofilepage/user-id/1416112)Thanks.

If I read it right, it looks like you're indiscriminately updating all of the cards pos values to the index (+1) of the array they end up in after sorting them. So your pos become 1, 2, ..., N?

Do you know what happens when a value you update a card pos to conflicts with another card that has the same pos value? Or, if the pos value of all of the cards in a list are not far enough away from each-other? I found some comments online speculating that Trello may change the value you give it or do a complete re-balancing of all of the card pos values if it doesn't like it or doesn't like the proximity spread.

I decided to go with sorting my cards and then if the new sorted set differs from the current pos set, sequentially, I do a full pass over the cards in the direction of new sort sequence and send them one by one to the "bottom" of their current list. After the pass is complete, the cards should be in the correct order. This leaves the responsibility for calculating the actual pos value in the API. That seems to be working.


author: Iain Dooley
body: [@Billy Stidham](/t5/user/viewprofilepage/user-id/3578634) If there were a conflict, it would be resolved as a result of the conflicting card having it's position set later on (because there can only be one card for each position in the array anyway).

Trello's internal representation of the position is different, they don't come back as integers, but you can set them as integers and it just seems to deal with it :)


Tags: api