Best logic for ordering an array of items based on a value

Viewed 50

I'm currently working on a simple drag and drop to reorder some items in a list, and what appeared to be a simple task turned into an endless debate.

Here's a bit of context:

  • I have a list of Items. Each item has a property named "position" (int, string or float, doesn't matter, I'll come back to this later)
  • Every time I move an item, I want to store its new position in my database, which mean I have to compute and provide said new position

Now, here are the many solutions I found around

EASY

Position is an int. Every time I move an item, I recompute the position of every affected view. Let's say I moved a view from position 7 to position 3, its new position will be 3, and every view with a position > 3 will have its position incremented

The only good thing about this is for readability. It's way easier to deal with ints when dealing with orders.

Besides that, it's a mess: a lot of operations, so if we have 10k items, and decide to move an item from last to first, it means 10k operations on my database. Yuck.

INTERMEDIATE

Position is a float. Every time I move an item, its new position will be the midpoint between the positions of its neighbors.

This makes things easier, but we might end up with very long floats (after a few reorderings, we might start to see some ugly 0.7853295325 laying around)

HARD

Using lexorank. Long story short, this means that position is now a string, and every time we move an item, we recompute this string. So first position is AAAAA, second is BBBBB, and so on, and if I move an item between these two, its position becomes ABBBB

This works, but I know for a certainty that I will not be dealing with a large number of items, so this might be a bit overkill.

So here are my questions:

  • What method do you use when reordering small lists of items?
  • Do you store your logic in the front or back end?
0 Answers
Related