对对象数组中的嵌套数组进行排序

Sort nested arrays in array of object

我不知道为什么,但是排序是每次让我感到困惑的编程事情之一...

我想对第一级的数组成员进行排序,所有具有属性 licence: "truck" 和 active: true 的成员首先在列表。那么接下来的成员应该是所有拥有 licence: "car" 和 active: true

的成员

underscore.js可用...

[
        {
            name: 'Denes',
            properties: [
                {
                    licence: "car",
                    active: true
                },
                {
                    licence: "truck",
                    active: false
                },
            ]
        },
        {
            name: 'Patrick',
            properties: [
                {
                    licence: "car",
                    active: false
                },
                {
                    licence: "truck",
                    active: true
                },
            ]
        },
        {
            name: 'Marc',
            properties: [
                {
                    licence: "car",
                    active: false
                },
                {
                    licence: "truck",
                    active: false
                },
            ]
        }
    ]

预期结果:

[

        {
            name: 'Patrick',
            properties: [
                {
                    licence: "car",
                    active: false
                },
                {
                    licence: "truck",
                    active: true
                },
            ]
        },
        {
            name: 'Denes',
            properties: [
                {
                    licence: "car",
                    active: true
                },
                {
                    licence: "truck",
                    active: false
                },
            ]
        },
        {
            name: 'Marc',
            properties: [
                {
                    licence: "car",
                    active: false
                },
                {
                    licence: "truck",
                    active: false
                },
            ]
        }
    ]

如果有很多人具有相同的属性,则此排序的复合键将是与排序顺序相关的活动属性的总和 + 姓名。这给出:

sortOrder = {'truck':1 , 'car': 2}

result = _.sortBy(data, item => [
    _(item.properties)
        .filter(prop => prop.active)
        .map(prop => sortOrder[prop.licence])
        .sum()
    ,
    item.name]
).reverse();

Array#sort 排序优先级函数会有所帮助。

var arr = [{ name: 'Denes', properties: [{ licence: "car", active: true }, { licence: "truck", active: false }, ] }, { name: 'Patrick', properties: [{ licence: "car", active: false }, { licence: "truck", active: true }, ] }, { name: 'Marc', properties: [{ licence: "car", active: false }, { licence: "truck", active: false }, ] }];

arr.sort(function (a, b) {
    function max(r, a) {
        return Math.max(r, ({ truck: 2, car: 1 }[a.licence] || 0) + ({ true: 8, false: 4 }[a.active] || 0));
    }
    return b.properties.reduce(max, 0) - a.properties.reduce(max, 0);
})

console.log(arr);

试试这个:

function comparator(){
    return function(a, b){
        return weightage(b)- weightage(a);
    }
};

function weightage(obj){
    var maxWeight = -1;
    for (var i in obj.properties){
        if(obj.properties[i].licence == 'truck' && obj.properties[i].active) {
            maxWeight = 1;
            return maxWeight;
        } else if (obj.properties[i].licence == 'car' && obj.properties[i].active) {
            maxWeight = 0;
        }
    }
    return maxWeight;
};

假设您的数组被命名为:arr call arr.sort(comparator())。 希望这能解决您的疑问.. :)