简体   繁体   English

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

[英]Sort nested arrays in array of object

I don't know why, but sorting is one of the things by programming that confuses me every time... 我不知道为什么,但是排序是每次让我感到困惑的编程问题之一。

I want to sort the array members on the first level so, that all members with the properties licence: "truck" and active: true are at first in the list. 我想在第一层对数组成员进行排序,以便所有具有属性licence:“ truck”和active:true的成员都首先出现在列表中。 Then the next members should be all members with licence: "car" and active: true 然后,下一个成员应该是所有拥有执照的会员:“汽车”,活跃会员:true

underscore.js is avaiable... 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
                },
            ]
        }
    ]

Expected result: 预期结果:

[

        {
            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
                },
            ]
        }
    ]

A compound key for this sort would be the sum of active properties with respect to the sort order + the name, in case there are many people with equal properties. 如果有许多人具有相同的属性,则此排序的复合键将是与排序顺序有关的活动属性的总和+名称。 That gives: 这给出了:

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 and a function for a sort priority would help. 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); 

Try this: 尝试这个:

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

assuming your array is named: arr call arr.sort(comparator()) . 假设您的数组命名为: arr调用arr.sort(comparator()) Hope this solves your query.. :) 希望这可以解决您的查询.. :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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