javascriptsortingtreetable

How to order an array where a property is equal to another property Javascipt


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,

So if the item is on the top, his LeftNeighbourTaskUUID would be null, and if the item is bottom, the LeftNeighbourTaskUUID would be null, and in between, each items must be ordered where his UUID is equal to the RightNeighbourTaskUUID of the next item in the array

So each child item can have one uniq LeftNeighbourTaskUUID and/or unique RightNeighbourTaskUUID, or those can be null if it's a uniq child .

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

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

    //This will simply explore my children's noeuds recursively 
        ExploreAndSortChildrensInTree: function (people) {
            if (people.childrens.length > 1) {
                //Basically I want to apply sorting here 
                people.childrens = this.swap(people.childrens);
            }
            for (var i = 0; i < people.childrens.length; i++) {

                if (people.childrens[i].childrens.length > 0) {
                    if (people.childrens[i].childrens.length > 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]);
                }
            }
        }

I have no issues navigating threw each child items, but I'm unable to sort by swapping 😕:

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?

I've baked this function but it's so "not good" as it doesn't work in all use cases

swap: function (itemArr) {
        let sortedArray = [];
        let unsortedArr = _.cloneDeep(itemArr);
        let originalArray = _.sortBy(unsortedArr, o => o.LeftNeighbourTaskUUID !== null);
        sortedArray.push(originalArray[0]);
        let lastIndex = _.remove(originalArray, em => {
            return em.RightNeighbourTaskUUID === null;
        });
        for (i = 0; i < originalArray.length; i++) {
            let nextElement = _.find(originalArray, awp => {
                return awp.UUID === originalArray[i].RightNeighbourTaskUUID;
            });
            if (typeof nextElement !== 'undefined') {
                sortedArray.push(nextElement);
            }
        }
        sortedArray.push(lastIndex[0]);
    

    return sortedArray;
        }, 
 

Below, is a simple json data of my array

 [
      {
        "LeftNeighbourTaskUUID": "00163E6A-6186-1EDA-8F8C-69F3B4864EC5",
        "RightNeighbourTaskUUID": "00163E6A-610A-1EDA-ACCD-2ABD6F12CC4A",
        "UUID": "00163E6A-6186-1EDA-8F8C-69F3B4880EC5",
        "Name": "I eat Potato",
        "childrens": [
          {
            "LeftNeighbourTaskUUID": "00163E6A-6186-1EDA-8F8C-69F3B4880EC5",
            "RightNeighbourTaskUUID": null,
            "UUID": "00163E6A-610A-1EDA-ACCD-ABC",
            "Name": "I eat A LOT Potato",
            "childrens": [
              {
                "LeftNeighbourTaskUUID": null,
                "RightNeighbourTaskUUID": null,
                "UUID": "ABC-610A-1EDA-ACCD-ABC",
                "Name": "Lonely potato",
                "childrens": []
              }
            ]
          },
  {
            "LeftNeighbourTaskUUID": null,
            "RightNeighbourTaskUUID": "00163E6A-610A-1EDA-ACCD-ABC",
            "UUID": "00163E6A-6186-1EDA-8F8C-69F3B4880EC5",
            "Name": "I eat too much Potato",
            "childrens": []
          },
        ]
      },
      {
        "LeftNeighbourTaskUUID": "00163E6A-6186-1EDA-8F8C-665581CA8EC4",
        "RightNeighbourTaskUUID": "00163E6A-6186-1EDA-8F8C-69F3B4880EC5",
        "UUID": "00163E6A-6186-1EDA-8F8C-69F3B4864EC5",
        "Name": "Consultant",
        "childrens": [
          {
            "LeftNeighbourTaskUUID": null,
            "RightNeighbourTaskUUID": null,
            "UUID": "00163E6A-6186-1EDA-8F8C-69F3B4864EC5",
            "Name": "Consultant",
            "childrens": []
          }
        ]
      },
      {
        "LeftNeighbourTaskUUID": "00163E6A-610A-1EDA-ACCD-2ABD6F12CC4A",
        "RightNeighbourTaskUUID": null,
        "UUID": "00163E6A-610A-1EEA-B0D7-E962CE710C24",
        "Name": "Tesla",
        "childrens": []
      },
      {
        "LeftNeighbourTaskUUID": "00163E6A-6186-1EDA-8F8C-69F3B4880EC5",
        "RightNeighbourTaskUUID": "00163E6A-610A-1EEA-B0D7-E962CE710C24",
        "UUID": "00163E6A-610A-1EDA-ACCD-2ABD6F12CC4A",
        "Name": "Bitcoin",
        "childrens": []
      },
      {
        "LeftNeighbourTaskUUID": null,
        "RightNeighbourTaskUUID": "00163E6A-6186-1EDA-8F8C-69F3B4864EC5",
        "UUID": "00163E6A-6186-1EDA-8F8C-665581CA8EC4",
        "Name": "Gestionnaire de projet",
        "childrens": []
      }
    ]

So my desired Output :

   [
  {
    "LeftNeighbourTaskUUID": null,
    "RightNeighbourTaskUUID": "00163E6A-6186-1EDA-8F8C-69F3B4864EC5",
    "UUID": "00163E6A-6186-1EDA-8F8C-665581CA8EC4",
    "Name": "Gestionnaire de projet",
    "childrens": []
  },
  {
    "LeftNeighbourTaskUUID": "00163E6A-6186-1EDA-8F8C-69F3B4864EC5",
    "RightNeighbourTaskUUID": "00163E6A-610A-1EDA-ACCD-2ABD6F12CC4A",
    "UUID": "00163E6A-6186-1EDA-8F8C-69F3B4880EC5",
    "Name": "I eat Potato",
    "childrens": [
      {
        "LeftNeighbourTaskUUID": null,
        "RightNeighbourTaskUUID": "00163E6A-610A-1EDA-ACCD-ABC",
        "UUID": "00163E6A-6186-1EDA-8F8C-69F3B4880EC5",
        "Name": "I eat too much Potato",
        "childrens": []
      },
      {
        "LeftNeighbourTaskUUID": "00163E6A-6186-1EDA-8F8C-69F3B4880EC5",
        "RightNeighbourTaskUUID": null,
        "UUID": "00163E6A-610A-1EDA-ACCD-ABC",
        "Name": "I eat A LOT Potato",
        "childrens": [
          {
            "LeftNeighbourTaskUUID": null,
            "RightNeighbourTaskUUID": null,
            "UUID": "ABC-610A-1EDA-ACCD-ABC",
            "Name": "Lonely potato",
            "childrens": []
          }
        ]
      }
    ]
  },
  {
    "LeftNeighbourTaskUUID": "00163E6A-6186-1EDA-8F8C-665581CA8EC4",
    "RightNeighbourTaskUUID": "00163E6A-6186-1EDA-8F8C-69F3B4880EC5",
    "UUID": "00163E6A-6186-1EDA-8F8C-69F3B4864EC5",
    "Name": "Consultant",
    "childrens": [
      {
        "LeftNeighbourTaskUUID": null,
        "RightNeighbourTaskUUID": null,
        "UUID": "00163E6A-6186-1EDA-8F8C-69F3B4864EC5",
        "Name": "Consultant",
        "childrens": []
      }
    ]
  },
  {
    "LeftNeighbourTaskUUID": "00163E6A-6186-1EDA-8F8C-69F3B4880EC5",
    "RightNeighbourTaskUUID": "00163E6A-610A-1EEA-B0D7-E962CE710C24",
    "UUID": "00163E6A-610A-1EDA-ACCD-2ABD6F12CC4A",
    "Name": "Bitcoin",
    "childrens": []
  },
  {
    "LeftNeighbourTaskUUID": "00163E6A-610A-1EDA-ACCD-2ABD6F12CC4A",
    "RightNeighbourTaskUUID": null,
    "UUID": "00163E6A-610A-1EEA-B0D7-E962CE710C24",
    "Name": "Tesla",
    "childrens": []
  }
]

enter image description here


Solution

  • I think this should do the trick:

    const child_x = 
      [ { LeftNeighbourTaskUUID  : '00163E6A-6186-1EDA-8F8C-69F3B4864EC5'
        , RightNeighbourTaskUUID : '00163E6A-610A-1EDA-ACCD-2ABD6F12CC4A'
        , UUID                   : '00163E6A-6186-1EDA-8F8C-69F3B4880EC5'
        , Name                   : 'I eat Potato'
        } 
      , { LeftNeighbourTaskUUID  : '00163E6A-6186-1EDA-8F8C-665581CA8EC4'
        , RightNeighbourTaskUUID : '00163E6A-6186-1EDA-8F8C-69F3B4880EC5'
        , UUID                   : '00163E6A-6186-1EDA-8F8C-69F3B4864EC5'
        , Name                   : 'Consultant'
        } 
      , { LeftNeighbourTaskUUID  : '00163E6A-610A-1EDA-ACCD-2ABD6F12CC4A'
        , RightNeighbourTaskUUID : null
        , UUID                   : '00163E6A-610A-1EEA-B0D7-E962CE710C24'
        , Name                   : 'Tesla'
        } 
      , { LeftNeighbourTaskUUID  : '00163E6A-6186-1EDA-8F8C-69F3B4880EC5'
        , RightNeighbourTaskUUID : '00163E6A-610A-1EEA-B0D7-E962CE710C24'
        , UUID                   : '00163E6A-610A-1EDA-ACCD-2ABD6F12CC4A'
        , Name                   : 'Bitcoin'
        } 
      , { LeftNeighbourTaskUUID  : null
        , RightNeighbourTaskUUID : '00163E6A-6186-1EDA-8F8C-69F3B4864EC5'
        , UUID                   : '00163E6A-6186-1EDA-8F8C-665581CA8EC4'
        , Name                   : 'Gestionnaire de projet'
        } 
      ] 
      
    const swapArrEl = (arr, x, y) =>{ arr[x] = arr.splice(y, 1, arr[x])[0] }
    
    let ref  = 0
      , max  = child_x.length
      , key  = 'LeftNeighbourTaskUUID'
      , kVal = null
      ;
    while (ref < max)
      {
      if (child_x[ref][key] !== kVal) 
      for (let i=ref +1; i<max; ++i)
        if (child_x[i][key] === kVal){ swapArrEl(child_x, ref, i); break }
      key = 'UUID'
      kVal = child_x[ref].RightNeighbourTaskUUID
      ++ref
      }
    
    console.log( child_x )
    .as-console-wrapper {max-height: 100%!important;top:0}