简体   繁体   中英

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. Then the next members should be all members with licence: "car" and active: true

underscore.js is avaiable...

[
        {
            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.

 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()) . Hope this solves your query.. :)

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