0

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
                },
            ]
        }
    ]
2
  • please add the result of the sorting. Commented May 25, 2016 at 10:07
  • Well, what have you tried? It's just a matter of using Array#sort and comparing the objects according to your criteria. Commented May 25, 2016 at 10:12

3 Answers 3

1

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.. :)

Sign up to request clarification or add additional context in comments.

1 Comment

1

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.