0
我有一個對象的數組:排序與父/子關係的多個屬性
a {bindSortorder: 4, bindParentid: -1, bindQuestionid: 371}
b {bindSortorder: 2, bindParentid: -1, bindQuestionid: 800}
c {bindSortorder: 4, bindParentid: 23, bindQuestionid: 123}
d {bindSortorder: 1, bindParentid: -1, bindQuestionid: 371}
e {bindSortorder: 4, bindParentid: 371, bindQuestionid: 456}
f {bindSortorder: 3, bindParentid: -1, bindQuestionid: 371}
g {bindSortorder: 2, bindParentid: 800, bindQuestionid: 223}
我需要將它們的結構如下排序:
排序順序
父
- Child(Questionid)
...因此,上面的例子將導致以下順序:
d b 克 ˚F 一個 ë Ç
如果它是必要的「-1」表示父母需要更改爲其他值,我願意接受。我只是試圖通過確保它不等於實際的Questionid而將其排除在外。
我已經寫了以下內容,它們成功排列了排序順序的優先次序,並將孩子們分組在一起....但他們沒有歸入他們各自的父母之下。然而,所有的父母都歸在一起。
return (aSortorder < bSortorder ? -1 : (aSortorder > bSortorder) ? 1 : ((aParentid < bParentid) ? -1 : ((aParentid > bParentid) ? 1 : 0)));
任何幫助,將不勝感激。由於
爲什麼權證之前?和b之前? –
對於通用算法,請參閱http://stackoverflow.com/questions/12467827/multi-level-parent-child-sorting –
E在C之前,因爲E = 371的殘差,使得E成爲A的孩子(其questionID爲371)將它們放在C之前 – Sabicas