简体   繁体   中英

How to double-sort a tree JSON object using JavaScript sort?

I need to sort z object first by param1 , then param2 . My attempt returns duplicated object instance, where object parameters are integer and non-unique.

How do I solve this problem?

z=[{object1},{object2},{object3},...,{objectN}];
z.sort(function(x,y){
    if(x.param1-y.param1>0){return -1};
    if(x.param1-y.param1<0){return 1};
    if(x.param1-y.param1==0){
        if(x.param2-y.param2>0){return -1};
        if(x.param2-y.param2<0){return 1};
    }; 

});

z is similar to:

z=[{"level":0,"order":0,"t":"section","a":{"class":"ro s-14 m-3"}},
    {"level":1,"order":0,"t":"h1","a":{"class":"tx-1 s-35"},"x":"This is a text"},
    {"level":1,"order":1,"t":"div","a":{"class":"tx-0 m-3"}},
        {"level":2,"order":0,"t":"h2","a":{"class":"tx-0 s-18"},"x":"This is a text"},
        {"level":2,"order":1,"t":"h3","a":{"class":"tx-0 s-14 fw-3"}},
            {"level":3,"order":0,"t":"li","x":"This is a text"},
            {"level":3,"order":1,"t":"li","x":"This is a text"},
            {"level":3,"order":2,"t":"li","x":"This is a text"},
            {"level":3,"order":3,"t":"li","x":"This is a text"},
            {"level":3,"order":4,"t":"li","x":"This is a text"},
            {"level":3,"order":5,"t":"li","x":"This is a text"},
            {"level":3,"order":6,"t":"li","x":"This is a text"},
            {"level":3,"order":7,"t":"li","x":"This is a text"},
            {"level":3,"order":8,"t":"li","x":"This is a text"},
        {"level":2,"order":2,"t":"h4","x":"This is a text"},
    {"level":1,"order":2,"t":"div","a":{"class":"tx-0 m-3"}},
        {"level":2,"order":0,"t":"h2","x":"This is a text"},
        {"level":2,"order":1,"t":"h3","a":{"class":"tx-0 s-12 fw-3"}},
            {"level":3,"order":0,"t":"li","x":"This is a text"},
            {"level":3,"order":1,"t":"li","x":"This is a text"},
            {"level":3,"order":2,"t":"li","x":"This is a text"},
            {"level":3,"order":3,"t":"li","x":"This is a text"},
            {"level":3,"order":4,"t":"li","x":"This is a text"},
            {"level":3,"order":5,"t":"li","x":"This is a text"},
            {"level":3,"order":6,"t":"li","x":"This is a text"},
            {"level":3,"order":7,"t":"li","x":"This is a text"},
            {"level":3,"order":8,"t":"li","x":"This is a text"},
            {"level":3,"order":9,"t":"li","x":"This is a text"},
            {"level":3,"order":10,"t":"li","x":"This is a text"},
            {"level":3,"order":11,"t":"li","x":"This is a text"},
        {"level":2,"order":2,"t":"h4","x":"This is a text"},
];

You implementation problem is for both param1 and param2 you doing the same if with different return value which is not correct. The correction of yours would be:

z = [ { object1 }, { object2 }, { object3 },...,{ objectN }];
z.sort(function(x,y){
    if(x.param1-y.param1>0){return -1};
    if(x.param1-y.param1<0){return 1};
    if(x.param1-y.param1==0){
        if(x.param2-y.param2>0){return -1};
        if(x.param2-y.param2<0){return 1};
    }; 

});

BTW I prefer this more than what you did:

 var items = [{ p1: 10, p2: 3 }, { p1: 9, p2: 4 }, { p1: 9, p2: 3 }, { p1: 10, p2: 2 }, { p1: 11, p2: 3 }, ]; items.sort((a, b) => (a.p1 - b.p1) || (a.p2 - b.p2)); console.log(items); 

As you can see difference of values is meaningful to sort function which allows you to pass result of subtract to be used for ordering. If difference is 0 (which means false too) then it will goes for second param and returns the result of their subtraction.

You could use a more compact callback for ascending order:

z.sort(function (x, y) {
    return x.param1 - y.param1 || x.param2 - y.param2;
});

or descending order:

z.sort(function (x, y) {
    return y.param1 - x.param1 || y.param2 - x.param2;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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