Reordering items in a RESTful way

后端 未结 2 2063
我在风中等你
我在风中等你 2021-02-15 02:40

I have a table that has the following data:

id position name
== ======== =========
1     4      Fred
2     2      Wilma
3     1      Pebbles
4     5      Barney
         


        
2条回答
  •  被撕碎了的回忆
    2021-02-15 03:11

    In order to do it efficiently use a hash table and never reorder everything just put them in between by changing their index key. What do I mean?

    Lets say

    1 a
    2 b
    3 c
    4 d
    5 e
    
    PUT /user/1/reorder/2
    

    Inside I would do, also the position is derived from the position in your index

    1  2   b
    2  2.5 a
    3  3   c
    4  4   d
    5  5 e
    

    As noted by Kajow, this approach has two issues:

    • After multiple reorders, float numbers might not suffice

    • Float number precision

    To solve them:

    • We don't use floats but big numbers 100000,200000,300000 etc and reorder, 200000,250000,30000,etc.

    • After x number of reorders we reset to 100000,200000,300000,400000. There might be a way to reorder only small parts every x divisions in order to keep this process simple, small and sporadic

提交回复
热议问题