title: Sorting Array of Reversible Arrays
body: <p>I came across a question during a timed leetcode type question generator, and frustrated I could not solve 100% of the test cases beyond some of the initial inputs.</p>
  <p>The question asks for a user to resort an array input so that arrays are sorted with like values 'touching' one another. Furthermore, inner arrays can be reversed so that a match can result. A result for which every inner array can be sorted is possible.</p>
  <p>For example, an array 'test = [[3,5],[4,2][5,2]]' could be sorted so that test[0] sits next to test[2], which can then match to test[1]. The final result being test = [[3,5],[5,2][2,4]].</p>
  <p>While a simple iterative solution that checks for a match, reverses the array and checks again, moves onto the next array, and then repeats until all arrays have been sorted, etc., works for initial test cases, I ran out of time to find a more dynamic solution that deals for more intricate test cases, including when the starting inner array is not optimal, and if their are more than one possible initial match, but only one that guarantees that the entire array can be sorted.</p>
  <p>Below is a sample of the original code submission that failed for later test cases:</p>
  <pre><code>// base test case
  test1 = [[3,5],[5,1],[2,7],[1,4],[4,2],[7,0]]
  
  //starting node shift test case
  test2 = [[5,1],[2,7],[1,4],[4,2],[7,0],[3,5]]
  
  //multiple node possibilities test case
  test3 = [[3,5],[5,1],[2,7],[1,4],[4,2],[9,5]]
  
  
  
  function organizer(arr) {
      let temp = arr.shift()
      let result = [temp[0], temp[1]]
      
      while (arr.length) {
          for (let i = 0; i &lt; arr.length; i++) {
              let current = arr[i]
              
              if (current[0] === result[result.length - 1]) {
                  result.push(current[1])
                  arr = arr.slice(0, i).concat(arr.slice(i + 1))
              } else if (current[1] === result[result.length - 1]) {
                  result.push(current[0])
                  arr = arr.slice(0, i).concat(arr.slice(i + 1))
              }
          }
      }
      
      return result
  }
  </code></pre>
  <p>Originally  I thought some back tracking solution may be needed to check all combinations, or that using permutations to produce all possible arrays and then returning an answer only when all possible inner arrays are sorted would be the answer...but I can't help thinking I'm missing a more obvious solution.</p>
  <p>What am I missing here?</p>