我面临这个问题,我想订购我的treeTable的每个子项目,但是如果RightNeighbourTaskUUID等于另一项的UUID,
因此,如果该项位于顶部,则其LeftNeighbourTaskUUID为空,如果该项为底部,则LeftNeighbourTaskUUID为空,在此之间,必须对每个项排序,其中其UUID等于数组中下一项的RightNeighbourTaskUUID。
因此,每个子项都可以有一个uniq LeftNeighbourTaskUUID和/或唯一的RightNeighbourTaskUUID,如果它是uniq子项目,则可以是null。
抱歉,我的MS绘图技巧(我在图片中使用ID、leftID和rightID只是为了帮助给出一个可视化的表示),只是为了帮助理解我的树表是如何构建的。
所以,我做了一个搜索和排序函数,如果孩子的长度大于1,我将探究孩子的排序,如果长度小于1,就不需要排序了。
//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]);
}
}
}对于抛出的每个子项,我没有问题,但是我无法通过交换进行排序:
是否有更好的方法来简单地排序基于RightID的每个子数组的ID等于下一个子数组的ID?
我已经烘焙了这个函数,但是它“不太好”,因为它并不适用于所有用例
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;
}, 下面是我的数组的一个简单的json数据
[
{
"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": []
}
]所以我想要的输出:
[
{
"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": []
}
]

发布于 2021-03-24 04:29:21
我认为这应该能起作用:
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}
发布于 2021-03-24 15:17:35
下一步只是在递归函数中重用此算法:
function treeOrdering( treeChilds )
{
let ref = 0
, max = treeChilds.length
, kVal = null
, keyR = 'LeftNeighbourTaskUUID' // reference key
, keyS = 'UUID' // search key
;
while (ref < max)
{
if (treeChilds[ref][keyR] !== kVal)
for (let i=ref +1; i<max; ++i)
if (treeChilds[i][keyR] === kVal)
{ swapArrEl(treeChilds, ref, i); break }
kVal = treeChilds[ref][keyS]
++ref
}
for (let tCs of treeChilds)
if (tCs.childrens.length) treeOrdering(tCs.childrens)
}演示代码:
const swapArrEl = (arr, x, y) =>{ arr[x] = arr.splice(y, 1, arr[x])[0] }
function treeOrdering( treeChilds )
{
let ref = 0
, max = treeChilds.length
, kVal = null
, keyR = 'LeftNeighbourTaskUUID' // reference key
, keyS = 'UUID' // search key
;
while (ref < max)
{
if (treeChilds[ref][keyR] !== kVal)
for (let i=ref +1; i<max; ++i)
if (treeChilds[i][keyR] === kVal)
{ swapArrEl(treeChilds, ref, i); break }
kVal = treeChilds[ref][keyS]
++ref
}
for (let tCs of treeChilds)
if (tCs.childrens.length) treeOrdering(tCs.childrens)
}
/***/
const data =
[ { 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 : // only one child
[ { LeftNeighbourTaskUUID : null
, RightNeighbourTaskUUID : null
, UUID : '00163E6A-6186-1EDA-8F8C-69F3B4864EC5'
, Name : 'Consultant'
, childrens : // with many childs to re-order
[ { LeftNeighbourTaskUUID : null
, RightNeighbourTaskUUID : null
, UUID : 'xxx1'
, Name : 'xxx1'
, childrens :
[ { LeftNeighbourTaskUUID : 'xxx_1_b'
, RightNeighbourTaskUUID : null
, UUID : 'xxx_1_c'
, Name : 'xxx_1_c'
, childrens : []
}
, { LeftNeighbourTaskUUID : null
, RightNeighbourTaskUUID : 'xxx_1_b'
, UUID : 'xxx_1_a'
, Name : 'xxx_1_a'
, childrens : []
}
, { LeftNeighbourTaskUUID : 'xxx_1_a'
, RightNeighbourTaskUUID : 'xxx_1_c'
, UUID : 'xxx_1_b'
, Name : 'xxx_1_b'
, 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 : []
}
]
/***/
treeOrdering(data)
console.log( data ).as-console-wrapper {max-height: 100%!important;top:0}
https://stackoverflow.com/questions/66773969
复制相似问题