I have an array of objects:
entities: [
{
name: "zBroomsticks PTY",
id: 34098365,
entityType: "personal",
facilities: [
{
type: "Home loan",
account: "032654 987465",
existing: true,
modified: "04/12/2018",
limit: 100000
},
{
type: "Credt card",
account: "032654 987465",
existing: false,
modified: "04/12/2018",
limit: 200000
},
{
type: "Credt card",
account: "032654 987465",
existing: false,
modified: "04/12/2018",
limit: 10000
},
{
type: "Credt card",
account: "032654 987465",
existing: false,
modified: "04/12/2018",
limit: 10000
}
]
},
{
name: "Mr John Doe -3409865, Mrs Jane Doe -34098365",
id: 34098365,
entityType: "business",
facilities: [
{
type: "Overdraft",
account: "032654 987465",
existing: false,
modified: "04/12/2018" ,
limit: 10000
}
]
},
{
name: "Mr Jack",
id: 34098365,
entityType: "mixed",
facilities: [
{
type: "Overdraft",
account: "032654 987465",
existing: false,
modified: "04/12/2018",
limit: 10000
}
]
}
]
I want to sort this in a specific order:
entity.name: in alphabetical ascending order.entity.entityType: 1. personal 2. business 3. mixedentity.facilities.limit: descending order
This is the code I've got so far:
sortData(entities) {
var order = {
entityType: { personal: 2, business: 1 }
};
return entities.map(ent =>
entities.sort(
(a, b) =>
a.name - b.name ||
order.entityType[a.facilities.entityType] - order.entityType[b.facilities.entityType]
)
);
}
I know how to perform the name sorting but cannot find an approach for 2 & 3?
limitis only to sort thefacilitiesarray, or do you expect it to affect the outer arrayentitiestoo?