I made a Sorter for Repeating Groups. It's not perfect. Need an Engineers brain

I found a way to build primitive sorting into my app by adding a “Display Order” field (of type “number”) into my table for a list of items I want to show the user.

The Display order value is set when the user adds a new item: by searching all items of the same type, finding the highest display order value (using order by during search and getting the display order value of the first item) and then setting the display order value of the new entry to the result of the above search + 1.

For reporting, I’m able to display the data by display order, and I’m able to modify the Display Order as well. An increment event on a button increases the display order by 1. A decrement event does the opposite.

I display the data to the user ordered by display order AND also by modified date (so that if two items with the same display order value are present, the most recently modified shows above others with the same display order value).

I could just leave it like this.

Two issues:

  1. Ideally there should never be the same display order value present twice in the database. If an item’s display order changes, the item above and below should have a corresponding change to its display order.

  2. If an item is deleted, it leaves gaps in the display order value. Ideally the display order of all items above this item should change accordingly.

I’m really unsure how I would go about this.

Any Engineers out there that can provide some insight?

Here is a video of my app to show you everything I described above:

What is the question in short?

On increment you should swap display order values of the current element and the next one, but this is a bit tricky in bubble as it has no such notion as local variable. The gaps between orders should be ok, you shouldn’t worry about them.
You would need additional technical field on the ordered item, let say OrderOld. Whenever you swap two items, you first backup their current order into corresponding fields and then swap:

  1. Item1.OrderOld = Item1.Order
  2. Item2.OrderOld = Item2.Order
  3. Result of step 1.Order = Result of step 2.OrderOld
  4. Result of step 2.Order = Result of step 1.OrderOld

There is also more efficient way without using additional field, but it’s more tricky and less resilient to race conditions.

1 Like

Ok. Thanks @vladimir.pak. I will try that.

How to effectively allow the user to sort a Repeating Group in Bubble using up/down arrows

List Shifter, Floppy, etc. Anything that lets you change the order of items in a list via an action.

What is that? A plugin? On my phone atm. Will google later

Ok List Shifter looks great. Thanks @keith