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
                },
            ]
        }
    ]
georg
  • 211,518
  • 52
  • 313
  • 390
Patrik
  • 1,119
  • 5
  • 18
  • 37

3 Answers3

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();
georg
  • 211,518
  • 52
  • 313
  • 390
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);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
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.. :)

seekers01
  • 560
  • 1
  • 4
  • 11
  • http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-a-bad-idea – georg May 25 '16 at 11:13