How to save a particular, mutable “order” into a database

后端 未结 9 1019
北恋
北恋 2021-02-05 03:34

Suppose I have some objects, and I want the user to be able to reorder them in any way they wish, say, by dragging them around. So I\'d have

  • Cheese
  • Muffin
9条回答
  •  灰色年华
    2021-02-05 04:06

    The best way I've found to handle this is to have a floating point order field. When you move something between two other items, set that field to halfway between its neighbors.

    This is cheap on both reads and writes. The only downside is the floats keep getting longer :)

提交回复
热议问题