需求:讲一段无序的的数组对象先按照日期排序,日期相同的再按照名称排序(按名称排序是为了将名称前缀相同的放在一起)
数据:
newFundlist: [
{
issueenddate: "2021-12-25",
issuestartdate: "2021-12-13",
secuname: "易方达互联互通ETF联接C",
},
{
issueenddate: "2021-12-25",
issuestartdate: "2021-12-13",
secuname: "天弘中证指数增强C",
},
{
issueenddate: "2021-12-25",
issuestartdate: "2021-12-13",
secuname: "天弘中证指数增强A",
},
{
issueenddate: "2021-12-25",
issuestartdate: "2021-12-13",
secuname: "易方达互联互通ETF联接A",
},
]
处理:
let arr = []
let sortedList = newFundlist.sort((a, b) => {
return new Date(a.issueenddate).getTime() - new Date(b.issueenddate).getTime()
})
let dateList = Object.values(sortedList.reduce((res, item) => {
res[item.issueenddate] ? res[item.issueenddate].push(item) : res[item.issueenddate] = [item];
return res;
}, {}));
dateList.forEach(item => {
let newArr = item.sort((a, b) => {
return a['secuname'].localeCompare(b['secuname'], 'zh-CN')
})
arr.push(...newArr)
})
console.log(arr)