title: algorithm reorder list of objects
body: <p>I am searching for an algorithm to reorder an list of dictionarys or in javascript array of objects.
  For example I have the following list of objects:</p>
  <pre><code>my_dict = [
      {
          &quot;id&quot;: 123,
          &quot;priority&quot;: 1
      },
      {
          &quot;id&quot;: 234,
          &quot;priority&quot;: 2
      },
      {
          &quot;id&quot;: 345,
          &quot;priority&quot;: 3
      },
      {
          &quot;id&quot;: 654,
          &quot;priority&quot;: 4
      }
  ]
  </code></pre>
  <p>Now I want to change the item with id 654 to be the first priority. So the other items gets automatically to priority 2,3 and 4.
  This should be the result:</p>
  <pre><code>my_dict = [
      {
          &quot;id&quot;: 654,
          &quot;priority&quot;: 1
      }
      {
          &quot;id&quot;: 123,
          &quot;priority&quot;: 2
      },
      {
          &quot;id&quot;: 234,
          &quot;priority&quot;: 3
      },
      {
          &quot;id&quot;: 345,
          &quot;priority&quot;: 4
      },
  ]
  </code></pre>
  <p>Another example is when I want to decrease the priority of item 234 to priority 3 then the item with priority 3 should become priority 2 (from the original/first dict). There are should no priority be twice in this list and there should not be gaps.</p>
  <pre><code>my_dict = [
      {
          &quot;id&quot;: 123,
          &quot;priority&quot;: 1
      },
      {
          &quot;id&quot;: 345,
          &quot;priority&quot;: 2
      },
      {
          &quot;id&quot;: 234,
          &quot;priority&quot;: 3
      },
      {
          &quot;id&quot;: 654,
          &quot;priority&quot;: 4
      }
  ]
  </code></pre>
  <p>For clarification here is an other example.
  If I move item with id 123 to priority 4 the item before should become priority 1,2 and 3.</p>
  <pre><code>my_dict = [
  
      {
          &quot;id&quot;: 234,
          &quot;priority&quot;: 1
      },
      {
          &quot;id&quot;: 345,
          &quot;priority&quot;: 2
      },
      {
          &quot;id&quot;: 654,
          &quot;priority&quot;: 3
      }
          {
          &quot;id&quot;: 123,
          &quot;priority&quot;: 4
      },
  ]
  </code></pre>
  <p>The list should always start with priority 1. Can somebody explain how I can implement it with python or javascript? I tried it with a for loop starting and 1 but this does not work.</p>