如何根据嵌套数组中的日期键对对象数组进行排序?
按通知最新日期对对象数组降序排序
我有如下对象数组:
var dic = [ {user:'John', notifications:[{created: 'Wed Jan 12 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]}, {user:'Bob', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}, {created: 'Wed Feb 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]}, {user:'Ron', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]}, {user:'xxx', notifications:[{created: 'Wed Jan 31 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]}, ]
我的目标是根据每个对象的notifications数组中的最新日期对原数组进行降序排序,最终结果中Bob应排在首位,因为他的通知里有最新的日期(2月15日),预期结果如下:
var dic = [ {user:'Bob', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}, {created: 'Wed Feb 15 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]}, {user:'xxx', notifications:[{created: 'Wed Jan 31 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]}, {user:'John', notifications:[{created: 'Wed Jan 12 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]}, {user:'Ron', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]}, ]
我尝试了如下代码进行排序,但未能成功:
var dic = [ {user:'John', notifications:[{created: 'Wed Jan 12 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]}, {user:'Bob', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}, {created: 'Wed Feb 15 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]}, {user:'Ron', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]}, {user:'xxx', notifications:[{created: 'Wed Jan 31 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]}, ] dic.sort((a,b) => { //console.log('a', a, 'b', b); if(a.notifications.length > 1){ const test = a.notifications.reduce((a,b) => new Date(a.created).getTime() >= new Date(b.created).getTime() ? a.created : b.created); return new Date(test).getTime() >= new Date(b.notifications[0].created).getTime() ? -1 : 1; } else{ return new Date(a.notifications[0].created).getTime() <= new Date(b.notifications[0].created).getTime() ? -1 : 1; } }) console.log(dic)
问题分析
原代码存在两个核心问题:
- 仅处理了
a的通知数组长度大于1的情况,未对b做同样处理,当b有多个通知时,仅取第一个通知的日期参与比较,导致逻辑错误 - 排序条件判断冗余且不严谨,没有统一获取每个对象的最新通知日期再进行比较
解决方案
先封装一个辅助函数,用来获取任意用户对象的最新通知时间戳:
function getLatestNotificationTime(user) { return user.notifications.reduce((maxTime, notification) => { const currentTime = new Date(notification.created).getTime(); return currentTime > maxTime ? currentTime : maxTime; }, 0); }
然后利用这个辅助函数进行排序:
var dic = [ {user:'John', notifications:[{created: 'Wed Jan 12 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]}, {user:'Bob', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}, {created: 'Wed Feb 15 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]}, {user:'Ron', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]}, {user:'xxx', notifications:[{created: 'Wed Jan 31 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]}, ]; dic.sort((a, b) => { const aLatest = getLatestNotificationTime(a); const bLatest = getLatestNotificationTime(b); // 降序排序:时间戳大的对象排在前面 return bLatest - aLatest; }); console.log(dic);
该方案通过辅助函数统一获取每个用户的最新通知时间戳,排序时直接用两个时间戳相减,利用数值差实现降序排序,逻辑简洁且严谨,能满足需求。
内容的提问来源于stack exchange,提问作者RoyBarOn
相关产品推荐
相关产品推荐

