SaadRH
SaadRH

Reputation: 150

Sorting an array of objects by property array

I got an array of objects like this one:

var SkillBuddys = [
    {
        Property1: "1",
        Property2: "Test",
        Property3: "Data",
        Property4: [{},{},{}],
    }, {
        Property1: "2",
        Property2: "Test2",
        Property3: "Data2"
    }, {
        Property1: "3",
        Property2: "Test3",
        Property3: "Data3",
        Property4: [{},{}],
    }, {
        Property1: "4",
        Property2: "Test4",
        Property3: "Data4"
    }, {
        Property1: "5",
        Property2: "Test5",
        Property3: "Data5",
        Property4: [{}],
    }
];

I want to sort it with javaScript on Property 4, so if the object has the property 4 and contains an array. Like this:

var SkillBuddys = [
    {
        Property1: "1",
        Property2: "Test",
        Property3: "Data",
        Property4: [{},{},{}],
    }, {
        Property1: "3",
        Property2: "Test3",
        Property3: "Data3",
        Property4: [{},{}],
    }, {
        Property1: "5",
        Property2: "Test5",
        Property3: "Data5",
        Property4: [{}],
    }, {
        Property1: "2",
        Property2: "Test2",
        Property3: "Data2"
    }, {
        Property1: "4",
        Property2: "Test4",
        Property3: "Data4"
    }
];

If Property 4 does not exists it returns "Undefined". How can i sort this with Array.Sort?

Upvotes: 4

Views: 108

Answers (3)

malarres
malarres

Reputation: 2946

SkillBuddys
.filter((x) => Array.isArray(x.Property4)  )
.sort( (a,b) => b.Property4.length - a.Property4.length)
.concat( SkillBuddys.filter((x) => !Array.isArray(x.Property4))

This will give you an array with the elements with Property4 sorted by lenght of the inner array, and with the rest of the elements afterwards in no specified ordering (you could pipe a sort() with an ordering of your choice)

Upvotes: 1

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48437

You could sort array by using length property and || operator.

var array = [ { Property1: "1", Property2: "Test", Property3: "Data", Property4: [{},{},{}], }, { Property1: "2", Property2: "Test2", Property3: "Data2" }, { Property1: "3", Property2: "Test3", Property3: "Data3", Property4: [{},{}], }, { Property1: "4", Property2: "Test4", Property3: "Data4" }, { Property1: "5", Property2: "Test5", Property3: "Data5", Property4: [{}], } ];

array.sort((a, b) => (b['Property4'] || []).length - (a['Property4'] || []).length);

console.log(array);

Upvotes: 3

Nick Parsons
Nick Parsons

Reputation: 50974

One approach could be to destructure and then use a default array if the property doesn't exist. You can then .sort() by the difference in the array lengths:

const skillBuddys = [ { Property1: "1", Property2: "Test", Property3: "Data", Property4: [{},{},{}], }, { Property1: "2", Property2: "Test2", Property3: "Data2" }, { Property1: "3", Property2: "Test3", Property3: "Data3", Property4: [{},{}], }, { Property1: "4", Property2: "Test4", Property3: "Data4" }, { Property1: "5", Property2: "Test5", Property3: "Data5", Property4: [{}], } ];

const res = skillBuddys.sort(
  ({Property4: a = []}, {Property4: b = []}) => b.length - a.length
);

console.log(res);

Upvotes: 2

Related Questions