[英]Sorting on multiple attributes with parent/child relationship
我有一個對象數組:
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}
我需要按以下結構對其進行排序:
排序
父級
...以便上面的示例將導致以下順序:
dbgfaec
如果有必要將表示父項的“ -1”更改為其他值,我願意接受。 我只是想通過確保它不能等於實際的Questionid來將其從排序方程中取出。
我寫了以下內容,這些內容成功地確定了排序順序的優先級並將這些孩子分組在一起。 但是,所有父母都是一起分組的。
return (aSortorder < bSortorder ? -1 : (aSortorder > bSortorder) ? 1 : ((aParentid < bParentid) ? -1 : ((aParentid > bParentid) ? 1 : 0)));
任何幫助,將不勝感激。 謝謝
這是我解決的方法:
SortQuestionsByParent:function(a,b){
var aElm = $(a).data();
var bElm = $(b).data();
var aParent = aElm.bindParentid;
var bParent = bElm.bindParentid;
var aQuestionId = aElm.bindQuestionid;
var bQuestionId = bElm.bindQuestionid;
var aOrder = aElm.bindSortorder;
var bOrder = bElm.bindSortorder;
var aTempId;
var bTempId;
(aParent < 0) ? aTempId = aQuestionId + ".5" : aTempId = aParent + ".9";
(bParent < 0) ? bTempId = bQuestionId + ".5" : bTempId = bParent + ".9";
var result = aOrder - bOrder || aTempId - bTempId;
console.log(result);
return result;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.