title: How to order an array where a property is equal to another property Javascipt
body: <p>I'm facing this issue, I would like to order each sub-items of my  treeTable but where the RightNeighbourTaskUUID is equal to the UUID of the other item,</p>
  <p>So if the item is on the top, his <strong>LeftNeighbourTaskUUID</strong> would be null, and if the item is bottom, the <strong>LeftNeighbourTaskUUID</strong> would be null, and in between, each  items must be ordered where his UUID is equal to the <strong>RightNeighbourTaskUUID</strong> of the next item in the array</p>
  <p>So each child item can have one uniq LeftNeighbourTaskUUID  and/or unique RightNeighbourTaskUUID, or those can be null if it's a uniq child .</p>
  <p>Sorry about my MS Paint skills,(I've used ID and leftID and rightID in the picture just to help give a visual representation) just to help understanding how my tree Table is built</p>
  <p>So I've made a search and sort function, where I will explore the children's and sort them if the children's length is higher than 1 , as no need to sort if the length is 1 or less</p>
  <pre><code>    //This will simply explore my children's noeuds recursively 
          ExploreAndSortChildrensInTree: function (people) {
              if (people.childrens.length &gt; 1) {
                  //Basically I want to apply sorting here 
                  people.childrens = this.swap(people.childrens);
              }
              for (var i = 0; i &lt; people.childrens.length; i++) {
  
                  if (people.childrens[i].childrens.length &gt; 0) {
                      if (people.childrens[i].childrens.length &gt; 1) {
  //No idea why I double check length.....but anyways too much coffee already
                          people.childrens[i].childrens = this.swap(people.childrens[i].childrens);
                      }
                      this.ExploreAndSortChildrensInTree(people.childrens[i]);
                  }
              }
          }
  </code></pre>
  <p>I have no issues navigating threw each child items, but I'm unable to sort by swapping :</p>
  <p>Is there a better way to simply sort each children's array based on the RightID is equal to the ID of the next one?</p>
  <p>I've baked this function but it's so &quot;not good&quot; as it doesn't work in all use cases</p>
  <pre><code>swap: function (itemArr) {
          let sortedArray = [];
          let unsortedArr = _.cloneDeep(itemArr);
          let originalArray = _.sortBy(unsortedArr, o =&gt; o.LeftNeighbourTaskUUID !== null);
          sortedArray.push(originalArray[0]);
          let lastIndex = _.remove(originalArray, em =&gt; {
              return em.RightNeighbourTaskUUID === null;
          });
          for (i = 0; i &lt; originalArray.length; i++) {
              let nextElement = _.find(originalArray, awp =&gt; {
                  return awp.UUID === originalArray[i].RightNeighbourTaskUUID;
              });
              if (typeof nextElement !== 'undefined') {
                  sortedArray.push(nextElement);
              }
          }
          sortedArray.push(lastIndex[0]);
      
  
      return sortedArray;
          }, 
   
  </code></pre>
  <p>Below, is a simple json data of my array</p>
  <pre><code> [
        {
          &quot;LeftNeighbourTaskUUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-69F3B4864EC5&quot;,
          &quot;RightNeighbourTaskUUID&quot;: &quot;00163E6A-610A-1EDA-ACCD-2ABD6F12CC4A&quot;,
          &quot;UUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-69F3B4880EC5&quot;,
          &quot;Name&quot;: &quot;I eat Potato&quot;,
          &quot;childrens&quot;: [
            {
              &quot;LeftNeighbourTaskUUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-69F3B4880EC5&quot;,
              &quot;RightNeighbourTaskUUID&quot;: null,
              &quot;UUID&quot;: &quot;00163E6A-610A-1EDA-ACCD-ABC&quot;,
              &quot;Name&quot;: &quot;I eat A LOT Potato&quot;,
              &quot;childrens&quot;: [
                {
                  &quot;LeftNeighbourTaskUUID&quot;: null,
                  &quot;RightNeighbourTaskUUID&quot;: null,
                  &quot;UUID&quot;: &quot;ABC-610A-1EDA-ACCD-ABC&quot;,
                  &quot;Name&quot;: &quot;Lonely potato&quot;,
                  &quot;childrens&quot;: []
                }
              ]
            },
    {
              &quot;LeftNeighbourTaskUUID&quot;: null,
              &quot;RightNeighbourTaskUUID&quot;: &quot;00163E6A-610A-1EDA-ACCD-ABC&quot;,
              &quot;UUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-69F3B4880EC5&quot;,
              &quot;Name&quot;: &quot;I eat too much Potato&quot;,
              &quot;childrens&quot;: []
            },
          ]
        },
        {
          &quot;LeftNeighbourTaskUUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-665581CA8EC4&quot;,
          &quot;RightNeighbourTaskUUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-69F3B4880EC5&quot;,
          &quot;UUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-69F3B4864EC5&quot;,
          &quot;Name&quot;: &quot;Consultant&quot;,
          &quot;childrens&quot;: [
            {
              &quot;LeftNeighbourTaskUUID&quot;: null,
              &quot;RightNeighbourTaskUUID&quot;: null,
              &quot;UUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-69F3B4864EC5&quot;,
              &quot;Name&quot;: &quot;Consultant&quot;,
              &quot;childrens&quot;: []
            }
          ]
        },
        {
          &quot;LeftNeighbourTaskUUID&quot;: &quot;00163E6A-610A-1EDA-ACCD-2ABD6F12CC4A&quot;,
          &quot;RightNeighbourTaskUUID&quot;: null,
          &quot;UUID&quot;: &quot;00163E6A-610A-1EEA-B0D7-E962CE710C24&quot;,
          &quot;Name&quot;: &quot;Tesla&quot;,
          &quot;childrens&quot;: []
        },
        {
          &quot;LeftNeighbourTaskUUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-69F3B4880EC5&quot;,
          &quot;RightNeighbourTaskUUID&quot;: &quot;00163E6A-610A-1EEA-B0D7-E962CE710C24&quot;,
          &quot;UUID&quot;: &quot;00163E6A-610A-1EDA-ACCD-2ABD6F12CC4A&quot;,
          &quot;Name&quot;: &quot;Bitcoin&quot;,
          &quot;childrens&quot;: []
        },
        {
          &quot;LeftNeighbourTaskUUID&quot;: null,
          &quot;RightNeighbourTaskUUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-69F3B4864EC5&quot;,
          &quot;UUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-665581CA8EC4&quot;,
          &quot;Name&quot;: &quot;Gestionnaire de projet&quot;,
          &quot;childrens&quot;: []
        }
      ]
  </code></pre>
  <p>So my desired Output :</p>
  <pre><code>   [
    {
      &quot;LeftNeighbourTaskUUID&quot;: null,
      &quot;RightNeighbourTaskUUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-69F3B4864EC5&quot;,
      &quot;UUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-665581CA8EC4&quot;,
      &quot;Name&quot;: &quot;Gestionnaire de projet&quot;,
      &quot;childrens&quot;: []
    },
    {
      &quot;LeftNeighbourTaskUUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-69F3B4864EC5&quot;,
      &quot;RightNeighbourTaskUUID&quot;: &quot;00163E6A-610A-1EDA-ACCD-2ABD6F12CC4A&quot;,
      &quot;UUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-69F3B4880EC5&quot;,
      &quot;Name&quot;: &quot;I eat Potato&quot;,
      &quot;childrens&quot;: [
        {
          &quot;LeftNeighbourTaskUUID&quot;: null,
          &quot;RightNeighbourTaskUUID&quot;: &quot;00163E6A-610A-1EDA-ACCD-ABC&quot;,
          &quot;UUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-69F3B4880EC5&quot;,
          &quot;Name&quot;: &quot;I eat too much Potato&quot;,
          &quot;childrens&quot;: []
        },
        {
          &quot;LeftNeighbourTaskUUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-69F3B4880EC5&quot;,
          &quot;RightNeighbourTaskUUID&quot;: null,
          &quot;UUID&quot;: &quot;00163E6A-610A-1EDA-ACCD-ABC&quot;,
          &quot;Name&quot;: &quot;I eat A LOT Potato&quot;,
          &quot;childrens&quot;: [
            {
              &quot;LeftNeighbourTaskUUID&quot;: null,
              &quot;RightNeighbourTaskUUID&quot;: null,
              &quot;UUID&quot;: &quot;ABC-610A-1EDA-ACCD-ABC&quot;,
              &quot;Name&quot;: &quot;Lonely potato&quot;,
              &quot;childrens&quot;: []
            }
          ]
        }
      ]
    },
    {
      &quot;LeftNeighbourTaskUUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-665581CA8EC4&quot;,
      &quot;RightNeighbourTaskUUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-69F3B4880EC5&quot;,
      &quot;UUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-69F3B4864EC5&quot;,
      &quot;Name&quot;: &quot;Consultant&quot;,
      &quot;childrens&quot;: [
        {
          &quot;LeftNeighbourTaskUUID&quot;: null,
          &quot;RightNeighbourTaskUUID&quot;: null,
          &quot;UUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-69F3B4864EC5&quot;,
          &quot;Name&quot;: &quot;Consultant&quot;,
          &quot;childrens&quot;: []
        }
      ]
    },
    {
      &quot;LeftNeighbourTaskUUID&quot;: &quot;00163E6A-6186-1EDA-8F8C-69F3B4880EC5&quot;,
      &quot;RightNeighbourTaskUUID&quot;: &quot;00163E6A-610A-1EEA-B0D7-E962CE710C24&quot;,
      &quot;UUID&quot;: &quot;00163E6A-610A-1EDA-ACCD-2ABD6F12CC4A&quot;,
      &quot;Name&quot;: &quot;Bitcoin&quot;,
      &quot;childrens&quot;: []
    },
    {
      &quot;LeftNeighbourTaskUUID&quot;: &quot;00163E6A-610A-1EDA-ACCD-2ABD6F12CC4A&quot;,
      &quot;RightNeighbourTaskUUID&quot;: null,
      &quot;UUID&quot;: &quot;00163E6A-610A-1EEA-B0D7-E962CE710C24&quot;,
      &quot;Name&quot;: &quot;Tesla&quot;,
      &quot;childrens&quot;: []
    }
  ]
  </code></pre>
  <p><a href="https://i.stack.imgur.com/iFVWD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iFVWD.png" alt="enter image description here" /></a></p>