title: Ordering permutations in javascript
body: <p>I have objects that have such fields <code>id</code> and <code>position</code>.</p>
  <pre><code>const items = [{id: 11, position: 1}, {id: 12, position: 2}, {id: 13, position: 3}, {id: 14, position: 4}, {id: 15, position: 5}, {id: 16, position: 6}];
  </code></pre>
  <p>These items are basically folders stacked one on top of another, it's possible to move these folders relative to each other which means changing their position property.</p>
  <p>I need a function like this:</p>
  <pre><code>const moveDir = (idIn: number, idOut: number, currentList: Dir[]): Dir[] =&gt; {
      // some code;
      return;
  }
  </code></pre>
  <p>that would return a new list of folders after the change has taken place. As for the <code>idIn</code> and <code>idOut</code> params: I want to drag a folder with id <code>idIn</code> to the position of a folder with id <code>idOut</code>.</p>
  <p><strong>Example:</strong></p>
  <pre><code>--------
  Folder 1
  --------
  Folder 2
  --------
  Folder 3
  --------
  Folder 4
  --------
  Folder 5
  --------
  </code></pre>
  <p>If <code>idIn = 2</code>, <code>idOut = 4</code>, the result should be:</p>
  <pre><code>--------
  Folder 1
  --------
  Folder 3
  --------
  Folder 4
  --------
  Folder 2
  --------
  Folder 5
  --------
  </code></pre>
  <p>If <code>idIn = 4</code>, <code>idOut = 2</code>, the result should be:</p>
  <pre><code>--------
  Folder 1
  --------
  Folder 4
  --------
  Folder 2
  --------
  Folder 3
  --------
  Folder 5
  --------
  </code></pre>
  <p>Any ideas on how to implement that behaviour would be helpful.</p>
  <p>EDIT:</p>
  <p>The initial list of objects looks like this:</p>
  <pre><code>const items = [{id: 11, position: 1}, {id: 12, position: 2}, {id: 13, position: 3}, {id: 14, position: 4}, {id: 15, position: 5}];
  </code></pre>
  <p>I know items ids and if I want to change two of them, I should be able to pass the ids of those items the positions of which I want to change.</p>
  <p><strong>The edited example:</strong></p>
  <pre><code>--------
  Folder 1 (id = 11, position = 1)
  --------
  Folder 2 (id = 12, position = 2)
  --------
  Folder 3 (id = 13, position = 3)
  --------
  Folder 4 (id = 14, position = 4)
  --------
  Folder 5 (id = 15, position = 5)
  --------
  </code></pre>
  <p>If I change Folder 2 and Folder 4, it means <code>idIn = 12</code> and <code>idOut = 14</code>.</p>
  <p>The result should be:</p>
  <pre><code>--------
  Folder 1 (id = 11, position = 1)
  --------
  Folder 3 (id = 13, position = 2)
  --------
  Folder 4 (id = 14, position = 3)
  --------
  Folder 2 (id = 12, position = 4)
  --------
  Folder 5 (id = 15, position = 5)
  --------
  </code></pre>
  <p>That's the function should return this list (ordered by position):</p>
  <pre><code>[{id: 11, position: 1}, {id: 13, position: 2}, {id: 14, position: 3}, {id: 12, position: 4}, {id: 15, position: 5}];
  </code></pre>