4

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?

0

3 Answers 3

3

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

2

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

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.