Reordering items in a RESTful way

后端 未结 3 862
滥情空心
滥情空心 2021-02-15 02:43

I have a table that has the following data:

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


        
3条回答
  •  走了就别回头了
    2021-02-15 02:58

    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

提交回复
热议问题