我有一个对象数组:
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
}
]
}
]
我想按特定顺序对此进行排序:
entity.name
:按字母升序。
entity.entityType
:1.个人2.业务3.混合
entity.facilities.limit
:降序
这是我到目前为止获得的代码:
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]
)
);
}
我知道如何执行名称排序,但是找不到2和3的方法?
答案 0 :(得分:3)
首先,您可以使用localeCompare()来订购names
。其次,entityType
数组中没有属性facilities
,但是您正在尝试访问它。现在,一种解决方案是首先使用Array.map()获得一个新数组,其中facilities
属性由limit
属性排序,然后可以对{{1}返回的新数组进行排序}首先是map()
,然后是names
属性,如下所示:
entityType
const input = [{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}]},{name:"Mr Jack",id:34098365,entityType:"personal",facilities:[{type:"Overdraft",account:"032654 987465",existing:false,modified:"04/12/2018",limit:10000}]}];
let order = {
entityType: {personal:1, business:2, mixed:3}
};
function sortData(entities)
{
let limitOrder = entities.map(e =>
{
e.facilities.sort((a, b) => b.limit - a.limit);
return e;
});
return limitOrder.sort((a, b) =>
{
return a.name.localeCompare(b.name) ||
order.entityType[a.entityType] - order.entityType[b.entityType];
});
}
console.log(sortData(input));
请注意,我使用不同的.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
复制了与name: "Mr Jack"
相关的对象,因此您可以看到当存在两个相等的entityType
对象时该算法的性能。
答案 1 :(得分:0)
尝试
eType = {personal:1, business:2, mixed:3};
entities.sort((a,b) => {
if(a.name>b.name) return 1;
if(a.name<b.name) return -1;
let et=eType[a.entityType]-eType[b.entityType];
return et;
})
entities.forEach(e=> e.facilities.sort((a,b)=> b.limit - a.limit ))
let 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
}
]
}
];
eType = {personal:1, business:2, mixed:3};
entities.sort((a,b) => {
if(a.name>b.name) return 1;
if(a.name<b.name) return -1;
let et=eType[a.entityType]-eType[b.entityType];
return et;
})
entities.forEach(e=> e.facilities.sort((a,b)=> b.limit - a.limit ))
console.log(entities);
答案 2 :(得分:0)
按照优先级1
,2
和最后3
的顺序对数组进行排序
2
条件,您可以使用index
对象将entityType
及其权重映射。
3
因为要按descending order
进行排序,所以您在limit
列表中找到了最小的facilities
值并将其与其他项目进行比较。
let 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
}
]
}
];
const entityIndex = { personal: 1, business: 2, mixed: 3 };
let result = entities.sort((a, b) => {
if (a.name > b.name) return -1;
if (a.name < b.name) return 1;
let et = entityIndex[a.entityType] - entityIndex[b.entityType];
if (et != 0) return et;
const aMinLimit = Math.min(...a.facilities.map(i => i.limit));
const bMinLimit = Math.min(...b.facilities.map(i => i.limit));
return bMinLimit - aMinLimit;
})
console.log(JSON.stringify(result));