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

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

    If you want to have them redisplayed in the same order AND you want them to be able to reordered at any time I don't think you can get away from storing some value that indicates display priority in the database. I've used the very approach you describe to order items in an FAQ, researchers associated with a grant, the order of items in a menu, ...

    0 讨论(0)
  • 2021-02-05 04:25

    Yup, in a relational database there is no order, it's one of the fundamental concepts. So there's just no way without a numerical value or something like that.

    0 讨论(0)
  • 2021-02-05 04:26

    In my applications read operations are going to happen much more frequently than writes. Go with the numerical value to indicate sort order and deal with the cost of reordering the items. This is more than made up for by the fact that you can efficiently retrieve the items in the correct order for display purposes (which in a typical application happens more frequently than resorting does).

    Also, as already mentioned, if you retrieve a subset of the data (filter on type or something) the remaining items are still in the proper sort order.

    Remember the mantra K.I.S.S.

    0 讨论(0)
提交回复
热议问题