title: How to arrange existing data and sort the sequence according to its priority
body: <p>I am creating a mobile application that could display and allow the user to sort the items awaiting delivery based on their priority (by moving up and down the items on the screen). And I am displaying the items based on priority (DESC). Every new item inserted is with a default priority of &quot;1&quot;. The user could &quot;shift&quot; the priority of an item to any of the sequences in the existing list. I am writing a function in PHP that could check the new arrangement of the edited priority of the item but I can't seem to figure out how would that work in relation to the database</p>
  <p>For example, I have 6 items with all default priority of 1. I could arrange it if only the item of any sequence is brought to the top or bottom of the list. How can I do it to resequence the priority in the database if the item is &quot;shift&quot; to the middle of the 2nd or 3rd of the list? How would the priority of all the items being sorted again?</p>
  <p>For example with default all default priority is &quot;1&quot;,</p>
  <p>I could arrange the priority sequence if the last item goes to the top
  by making the last item priority to &quot;2&quot;,</p>
  <p>How am I going to sort if I want the &quot;Item 5&quot; to be in the middle? For example below,</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Item</th>
  <th>Priority</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>6</td>
  <td>2</td>
  </tr>
  <tr>
  <td>1</td>
  <td>1</td>
  </tr>
  <tr>
  <td>2</td>
  <td>1</td>
  </tr>
  <tr>
  <td><strong>5</strong></td>
  <td><strong>1 (???)</strong></td>
  </tr>
  <tr>
  <td>3</td>
  <td>1</td>
  </tr>
  <tr>
  <td>4</td>
  <td>1</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>How could I sort the priority accordingly for all the items again? Even by comparing the DateTime inserted or updated would not be 100% workable as well. The sorting of items are dynamic, the number of items could be infinite and it could be adjusted to any sequence. Appreciate any suggestions! Thank you.</p>