Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is not an easy problem. If you have a low number of sortable elements, I would just reset all of them to their new order.</p> <p>Otherwise, it seems it would take just as much work or more to "test-and-set" to modify only the records that have changed.</p> <p>You could delegate this work to the client-side. Have the client maintain old-sort-order and new-sort-order and determine which row[sort-order]'s should be updated - then passes those tuples to the PHP-mySQL interface.</p> <p>You could enhance this method in the following way (<em>doesn't</em> require floats):</p> <ol> <li><p>If all sortable elements in a list are initialized to a sort-order according to their position in the list, set the sort-order of every element to something like row[sort-order] = row[sort-order * K] where K is some number > average number of times you expect the list to be reordered. O(N), N=number of elements, but increases insertion capacity by at least N*K with at least K open slots between each exiting pair of elements.</p></li> <li><p>Then if you want to insert an element between two others its as simple as changing its sort-order to be one that is > the lower element and &lt; the upper. If there is no "room" between the elements you can simply reapply the "spread" algorithm (1) presented in the previous paragraph. The larger K is, the less often it will be applied.</p></li> </ol> <p>The K algorithm would be selectively applied in the PHP script while the choosing of the new sort-order's would be done by the client (Javascript, perhaps).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload