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

后端 未结 9 1052
北恋
北恋 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:15

    Looking at Tony Andrew's and Mark's answers in particular, it seems I really have only two alternatives:

    • Saving a 'next' value, making the objects behave like a linked list (see Mark's answer)
      With this, changing the order is cheap, but I'd have to retrieve the items and then sort them by their 'next' value, which is expensive
    • Saving an 'order' value (see Tony Andrew's answer)
      This makes retrieving cheap but saving a new order potentially expensive, because in the worst case, I'd have to change all the order values. cletus points out that one could use a large number in the form of 2^n for the order multiplier.

    Meta: All of these answers are good and correct, which one should I choose as correct?

提交回复
热议问题