0

I'm trying to create a rearrangement of cards, lists like Trello.

I read about how it does here How does Trello handle rearrangement of cards, lists, checklists etc and here What does the POS actually mean in the Trello API

Each position starts with the value 65,535 and creates a new card or list with the last position + 65,535, for example:

Card List

{ "_id" : ObjectId("5e7ea117e13673ec0db43104"), "name" : "A", "position" : 65.535 }
{ "_id" : ObjectId("5e7ea117e13673ec0db43105"), "name" : "B", "position" : 131.071 }
{ "_id" : ObjectId("5e7ea117e13673ec0db43106"), "name" : "C", "position" : 196.607 }
{ "_id" : ObjectId("5e7ea117e13673ec0db43107"), "name" : "D", "position" : 262.143 }
{ "_id" : ObjectId("5e7ea117e13673ec0db43108"), "name" : "E", "position" : 327.679 }

If Card E is inserted at the top of Card A, it will be divided by 2 (65,535 / 2 = 32,767)

If Card A is inserted at bottom of Card E, it adds up to the value of 65,535 (327,679 + 65,535 = 393,214)

In the middle, the position average of two adjacent positions.

The problem is, how can I change any position to the penultimate position? between D and E? Or between A and B?

If card A is inserted between cards D and E, Or insert Card E between A and B what logic is it to do this?

Is there a better way to do this?

Braian Silva
  • 1,986
  • 1
  • 15
  • 21
  • So if you wanted to move `"name" : "A"` to another new position, How does your input to query look like ? – whoami - fakeFaceTrueSoul Mar 28 '20 at 01:39
  • Can be use ID of `"name":"A"` and use the new position `Cards.ChangePos(IDNameA, 262.143)`, or use ID of name A and use between two positions to insert, `Cards.ChangePos(IDNameA, 196.607,262.143)` between Card C and E. – Braian Silva Mar 28 '20 at 01:53
  • 1
    If you wanted to read data like that, at least you can try aggregation to achieve that but if you wanted to update collection then I would suggest to do this through code as MongoDB doesn't offer anything at least near to something thru which this can be easily done. Issue is you cannot insert docs in between existing docs !! If you wanted to see data in your DB as you wanted then you've to re-write all the needed docs in the order you want each time when a change occurred. Anyway why does order matter when you look at DB, when you retrieve sort on position & retrieve you would see what u want – whoami - fakeFaceTrueSoul Mar 28 '20 at 04:42

1 Answers1

1

You could also use Lexorank algorithm. That's how Jira is handling their ordering of cards https://www.youtube.com/watch?v=OjQv9xMoFbg&feature=youtu.be

You can find multiple implementations of Lexorank in GitHub. Here's for example one simple JavaScript implementation of Lexorank https://github.com/acadea/lexorank

valstu
  • 71
  • 2