如何使用JavaScript排序对树JSON对象进行双重排序?

时间:2016-07-20 19:09:24

标签: javascript arrays algorithm sorting

我需要先z,然后param1param2对象进行排序。我的尝试返回重复的对象实例,其中对象参数是整数且非唯一。

我该如何解决这个问题?

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类似于:

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"},
];

2 个答案:

答案 0 :(得分:1)

您可以使用更紧凑的回调来升序:

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

或降序:

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

答案 1 :(得分:0)

如果使用不同的返回值,那么param1param2的实现问题是相同的。你的更正将是:

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我比你做的更喜欢这个:

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);

正如您所看到的,值的差异对于排序函数是有意义的,它允许您传递减法结果以用于排序。如果差异为0(这也意味着假)那么它将进入第二个参数并返回它们的减法结果。