简体   繁体   English

对具有父子关系的多个属性进行排序

[英]Sorting on multiple attributes with parent/child relationship

I have a an array of objects: 我有一个对象数组:

    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}

I need to sort them in the following structure: 我需要按以下结构对其进行排序:

Sortorder 排序

  • Parent 父级

    • Child (Questionid) 儿童(Questionid)

...so that the above example would result in the following order: ...以便上面的示例将导致以下顺序:

dbgfaec dbgfaec

If it's necessary for the "-1" denoting a parent to be changed to some other value, I'm open to that. 如果有必要将表示父项的“ -1”更改为其他值,我愿意接受。 I was just trying to take it out of the sort equation by making sure it could not equal an actual Questionid. 我只是想通过确保它不能等于实际的Questionid来将其从排序方程中取出。

I've written the following which successfully prioritizes the sort order and groups the children together....but they are not grouped under their respective parents. 我写了以下内容,这些内容成功地确定了排序顺序的优先级并将这些孩子分组在一起。 All of the parents are grouped together, however. 但是,所有父母都是一起分组的。

    return (aSortorder < bSortorder ? -1 : (aSortorder > bSortorder) ? 1 : ((aParentid < bParentid) ? -1 : ((aParentid > bParentid) ? 1 : 0)));

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

Here's how I solved it: 这是我解决的方法:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM