Note that there are some explanatory texts on larger screens.

plurals
  1. POReordering items with multiple order criteria
    primarykey
    data
    text
    <p><strong>Scenario</strong>:</p> <ul> <li>list of photos</li> <li>every photo has the following properties <ul> <li><code>id</code></li> <li><code>sequence_number</code></li> <li><code>main_photo_bit</code></li> </ul></li> <li>the first photo has the <code>main_photo_bit</code> set to <code>1</code> (all others are <code>0</code>)</li> <li>photos are ordered by <code>sequence_number</code> (<em>which is arbitrary</em>)</li> <li>the main photo does not necessarily have the lowest <code>sequence_number</code> (before sorting)</li> </ul> <p>See the following table:</p> <pre><code>id, sequence_number, main_photo_bit 1 10 1 2 5 0 3 20 0 </code></pre> <p>Now you want to change the order by changing the sequence number and main photo bit.</p> <p><strong>Requirements after sorting:</strong></p> <ul> <li>the <code>sequence_number</code> of the first photo is not changed</li> <li>the <code>sequence_number</code> of the first photo is the lowest</li> <li>as less changes as possible</li> </ul> <p><strong>Examples:</strong></p> <p>Example #1 (second photo goes to the first position):</p> <pre><code>id, sequence_number, main_photo_bit 2 10 1 1 15 0 3 20 0 </code></pre> <p><em>This is what happened:</em></p> <ul> <li><em>id 1: new <code>sequence_number</code> and <code>main_photo_bit</code> set to <code>0</code></em></li> <li><em>id 2: old first photo (id 2) <code>sequence_number</code> and <code>main_photo_bit</code> set to <code>1</code></em></li> <li><em>id 3: nothing happens</em></li> </ul> <p>Example #2 (third photo to first position):</p> <pre><code>id, sequence_number, main_photo_bit 3 10 1 1 20 0 2 30 0 </code></pre> <p><em>This is what happened:</em></p> <ul> <li><em>id 1: new <code>sequence_number</code> bigger than first photo and <code>main_photo_bit</code> to <code>0</code></em></li> <li><em>id 2: new <code>sequence_number</code> bigger than newly generated second <code>sequence_number</code></em></li> <li><em>id 3: old first photo <code>sequence_number</code> and <code>main_photo_bit</code> set to <code>1</code></em></li> </ul> <p><strong>What is the best approach to calculate the steps needed to save the new order?</strong></p> <p><em>Edit:</em><br> The reason that I want as less updates as possible is because I want to sync it to an external service, which is a quite costly operation.<br> I already got a working prototype of the algorithm, but it fails in some edge cases. So instead of patching it up (which might work -- but it will become even more complex than it is already), I want to know if there are other (better) ways to do it.<br> In my version (in short) it orders the photos (changing <code>sequence_number</code>'s), and swaps the <code>main_photo_bit</code>, but it isn't sufficient to solve every scenario.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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