如何排序 属性 等于另一个 属性 Javascript 的数组
How to order an array where a property is equal to another property Javascipt
我遇到了这个问题,我想订购我的树的每个 sub-itemsTable,但是 RightNeighbourTaskUUID 等于其他项目的 UUID,
因此,如果项目在顶部,他的 LeftNeighbourTaskUUID 将为空,如果项目在底部,则 LeftNeighbourTaskUUID 将为为 null,并且在两者之间,必须对每个项目进行排序,其中他的 UUID 等于数组中下一个项目的 RightNeighbourTaskUUID
因此每个 child 项都可以有一个唯一的 LeftNeighbourTaskUUID and/or 唯一的 RightNeighbourTaskUUID,或者如果它是一个唯一的 child 则这些可以为 null。
抱歉我的 MS Paint 技能,(我在图片中使用了 ID 和 leftID 和 rightID 只是为了帮助提供视觉表示)只是为了帮助理解我的树 Table 是如何构建的
所以我做了一个搜索和排序功能,如果 children 的长度大于 1,我将在其中探索 children 并对它们进行排序,因为如果长度小于或等于 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]);
}
}
}
我在导航时没有遇到任何问题 child 项目,但我无法通过交换进行排序:
有没有更好的方法可以简单地根据RightID等于下一个的ID对每个children的数组进行排序?
我已经烘焙了这个功能,但它太“不好”了,因为它不适用于所有用例
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": []
}
]
我认为这应该可以解决问题:
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}
下一步只是在递归函数中重新使用该算法:
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)
}
demo code:
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}
我遇到了这个问题,我想订购我的树的每个 sub-itemsTable,但是 RightNeighbourTaskUUID 等于其他项目的 UUID,
因此,如果项目在顶部,他的 LeftNeighbourTaskUUID 将为空,如果项目在底部,则 LeftNeighbourTaskUUID 将为为 null,并且在两者之间,必须对每个项目进行排序,其中他的 UUID 等于数组中下一个项目的 RightNeighbourTaskUUID
因此每个 child 项都可以有一个唯一的 LeftNeighbourTaskUUID and/or 唯一的 RightNeighbourTaskUUID,或者如果它是一个唯一的 child 则这些可以为 null。
抱歉我的 MS Paint 技能,(我在图片中使用了 ID 和 leftID 和 rightID 只是为了帮助提供视觉表示)只是为了帮助理解我的树 Table 是如何构建的
所以我做了一个搜索和排序功能,如果 children 的长度大于 1,我将在其中探索 children 并对它们进行排序,因为如果长度小于或等于 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]);
}
}
}
我在导航时没有遇到任何问题 child 项目,但我无法通过交换进行排序:
有没有更好的方法可以简单地根据RightID等于下一个的ID对每个children的数组进行排序?
我已经烘焙了这个功能,但它太“不好”了,因为它不适用于所有用例
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": []
}
]
我认为这应该可以解决问题:
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}
下一步只是在递归函数中重新使用该算法:
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)
}
demo code:
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}