按不同的键重新排序已排序的数组

我有这个数组,已经按上传时间排序



    [

      { id: 1133, userId: 101697, uploadTime: '2020-09-14T16:19:16.000Z' },

      { id: 1132, userId: 101543, uploadTime: '2020-09-14T16:17:27.000Z' },

      { id: 1131, userId: 101697, uploadTime: '2020-09-14T16:09:49.000Z' },

      { id: 1130, userId: 101697, uploadTime: '2020-09-14T16:09:16.000Z' },

      { id: 1128, userId: 101543, uploadTime: '2020-09-09T21:48:46.000Z' },

      { id: 1127, userId: 101543, uploadTime: '2020-09-09T21:41:41.000Z' },

      { id: 1126, userId: 101543, uploadTime: '2020-09-09T21:35:37.000Z' },

      { id: 1125, userId: 101543, uploadTime: '2020-09-09T19:41:57.000Z' },

      { id: 1082, userId: 101577, uploadTime: '2020-08-21T15:24:35.000Z' },

      { id: 1049, userId: 101589, uploadTime: '2020-08-15T23:31:42.000Z' },

      { id: 1040, userId: 101589, uploadTime: '2020-08-14T15:04:01.000Z' },

      { id: 1036, userId: 101589, uploadTime: '2020-08-13T00:09:43.000Z' },

      { id: 1035, userId: 101527, uploadTime: '2020-08-12T20:07:34.000Z' },

      { id: 1034, userId: 101612, uploadTime: '2020-08-12T17:33:26.000Z' },

      { id: 996, userId: 101589, uploadTime: '2020-08-11T15:20:11.000Z' }, 

      { id: 889, userId: 101626, uploadTime: '2020-08-06T14:47:18.000Z' }, 

      { id: 864, userId: 101589, uploadTime: '2020-08-05T15:23:50.000Z' }, 

      { id: 863, userId: 101589, uploadTime: '2020-08-05T14:42:14.000Z' }, 

      { id: 852, userId: 101589, uploadTime: '2020-08-04T18:05:07.000Z' }, 

      { id: 851, userId: 101589, uploadTime: '2020-08-04T17:57:58.000Z' }

    ]


现在我想求助于它,但仅适用于具有相同 userId 且 uploadTime 距当前时间不到 24 小时的对象。输出应该是这样的:



    [

      { id: 1133, userId: 101697, uploadTime: '2020-09-14T16:19:16.000Z' },

      { id: 1131, userId: 101697, uploadTime: '2020-09-14T16:09:49.000Z' },

      { id: 1130, userId: 101697, uploadTime: '2020-09-14T16:09:16.000Z' },

      { id: 1132, userId: 101543, uploadTime: '2020-09-14T16:17:27.000Z' },

    ]


叮当猫咪
浏览 156回答 4
4回答

holdtom

现在我想求助于它,但仅适用于具有相同 userId 且 uploadTime 距当前时间不到 24 小时的对象。这次排序需要稍微改变一下(看代码中的注释):如果两个日期都小于 24 小时,则按userId排序,否则继续使用 uploadTime 以保留顺序。var data = [&nbsp; &nbsp; {id: 1133, userId: 101697, uploadTime: '2020-09-14T16:19:16.000Z'},&nbsp; &nbsp; {id: 1132, userId: 101543, uploadTime: '2020-09-14T16:17:27.000Z'},&nbsp; &nbsp; {id: 1131, userId: 101697, uploadTime: '2020-09-14T16:09:49.000Z'},&nbsp; &nbsp; {id: 1130, userId: 101697, uploadTime: '2020-09-14T16:09:16.000Z'},&nbsp; &nbsp; {id: 1128, userId: 101543, uploadTime: '2020-09-09T21:48:46.000Z'},&nbsp; &nbsp; {id: 1127, userId: 101543, uploadTime: '2020-09-09T21:41:41.000Z'},&nbsp; &nbsp; {id: 1126, userId: 101543, uploadTime: '2020-09-09T21:35:37.000Z'},&nbsp; &nbsp; {id: 1125, userId: 101543, uploadTime: '2020-09-09T19:41:57.000Z'},&nbsp; &nbsp; {id: 1082, userId: 101577, uploadTime: '2020-08-21T15:24:35.000Z'},&nbsp; &nbsp; {id: 1049, userId: 101589, uploadTime: '2020-08-15T23:31:42.000Z'},&nbsp; &nbsp; {id: 1040, userId: 101589, uploadTime: '2020-08-14T15:04:01.000Z'},&nbsp; &nbsp; {id: 1036, userId: 101589, uploadTime: '2020-08-13T00:09:43.000Z'},&nbsp; &nbsp; {id: 1035, userId: 101527, uploadTime: '2020-08-12T20:07:34.000Z'},&nbsp; &nbsp; {id: 1034, userId: 101612, uploadTime: '2020-08-12T17:33:26.000Z'},&nbsp; &nbsp; {id: 996, userId: 101589, uploadTime: '2020-08-11T15:20:11.000Z'},&nbsp; &nbsp; {id: 889, userId: 101626, uploadTime: '2020-08-06T14:47:18.000Z'},&nbsp; &nbsp; {id: 864, userId: 101589, uploadTime: '2020-08-05T15:23:50.000Z'},&nbsp; &nbsp; {id: 863, userId: 101589, uploadTime: '2020-08-05T14:42:14.000Z'},&nbsp; &nbsp; {id: 852, userId: 101589, uploadTime: '2020-08-04T18:05:07.000Z'},&nbsp; &nbsp; {id: 851, userId: 101589, uploadTime: '2020-08-04T17:57:58.000Z'}];data.sort(function (a, b) {&nbsp; &nbsp; // compute the difference with current date&nbsp; &nbsp; var dcta = (new Date()).getTime() - (new Date(a.uploadTime)).getTime();&nbsp; &nbsp; var dctb = (new Date()).getTime() - (new Date(b.uploadTime)).getTime();&nbsp; &nbsp;// convert then in hours&nbsp; &nbsp; var h1 = (((dctb) / 1000) / 60)/60;&nbsp; &nbsp; var h2 = ((dctb / 1000) / 60)/60;&nbsp; &nbsp; // if both are less than 24hours....&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if (h1 <=24 && h2 <= 24) {&nbsp; &nbsp; &nbsp; &nbsp; return b.userId - a.userId;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return (new Date(b.uploadTime)).getTime() - (new Date(a.uploadTime)).getTime();&nbsp; &nbsp; }});console.log(JSON.stringify(data).replace(/\},/g, '},\n'));

小唯快跑啊

let testArray = [&nbsp; &nbsp; { id: 1133, userId: 101697, uploadTime: '2020-09-14T16:19:16.000Z' },&nbsp; &nbsp; { id: 1132, userId: 101543, uploadTime: '2020-09-14T16:17:27.000Z' },&nbsp; &nbsp; { id: 1131, userId: 101697, uploadTime: '2020-09-14T16:09:49.000Z' },&nbsp; &nbsp; { id: 1130, userId: 101697, uploadTime: '2020-09-14T16:09:16.000Z' },&nbsp; &nbsp; { id: 1128, userId: 101543, uploadTime: '2020-09-09T21:48:46.000Z' },&nbsp; &nbsp; { id: 1127, userId: 101543, uploadTime: '2020-09-09T21:41:41.000Z' },&nbsp; &nbsp; { id: 1126, userId: 101543, uploadTime: '2020-09-09T21:35:37.000Z' },&nbsp; &nbsp; { id: 1125, userId: 101543, uploadTime: '2020-09-09T19:41:57.000Z' },&nbsp; &nbsp; { id: 1082, userId: 101577, uploadTime: '2020-08-21T15:24:35.000Z' },&nbsp; &nbsp; { id: 1049, userId: 101589, uploadTime: '2020-08-15T23:31:42.000Z' },&nbsp; &nbsp; { id: 1040, userId: 101589, uploadTime: '2020-08-14T15:04:01.000Z' },&nbsp; &nbsp; { id: 1036, userId: 101589, uploadTime: '2020-08-13T00:09:43.000Z' },&nbsp; &nbsp; { id: 1035, userId: 101527, uploadTime: '2020-08-12T20:07:34.000Z' },&nbsp; &nbsp; { id: 1034, userId: 101612, uploadTime: '2020-08-12T17:33:26.000Z' },&nbsp; &nbsp; { id: 996, userId: 101589, uploadTime: '2020-08-11T15:20:11.000Z' },&nbsp; &nbsp; { id: 889, userId: 101626, uploadTime: '2020-08-06T14:47:18.000Z' },&nbsp; &nbsp; { id: 864, userId: 101589, uploadTime: '2020-08-05T15:23:50.000Z' },&nbsp; &nbsp; { id: 863, userId: 101589, uploadTime: '2020-08-05T14:42:14.000Z' },&nbsp; &nbsp; { id: 852, userId: 101589, uploadTime: '2020-08-04T18:05:07.000Z' },&nbsp; &nbsp; { id: 851, userId: 101589, uploadTime: '2020-08-04T17:57:58.000Z' }];testArray = testArray.sort((x, y) => {&nbsp; &nbsp; return (y.userId - x.userId);});testArray = testArray.filter(currentObj => {&nbsp; &nbsp; return (new Date() - new Date(currentObj.uploadTime)) < 24 * 60 * 60 * 1000;})&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; console.log(testArray);&nbsp;我们排序比较对象的两个不同属性。现在您将按 userId 对用户进行排序,然后按从最新到最旧的上传时间排序。结果:[ { id: 1133,&nbsp; &nbsp; userId: 101697,&nbsp; &nbsp; uploadTime: '2020-09-14T16:19:16.000Z' },&nbsp; { id: 1131,&nbsp; &nbsp; userId: 101697,&nbsp; &nbsp; uploadTime: '2020-09-14T16:09:49.000Z' },&nbsp; { id: 1130,&nbsp; &nbsp; userId: 101697,&nbsp; &nbsp; uploadTime: '2020-09-14T16:09:16.000Z' },&nbsp; { id: 1132,&nbsp; &nbsp; userId: 101543,&nbsp; &nbsp; uploadTime: '2020-09-14T16:17:27.000Z' } ]

哔哔one

var uploads=[{"id":1035,"userId":101527,"uploadTime":"2020-08-12T20:07:34.000Z"},{"id":1125,"userId":101543,"uploadTime":"2020-09-09T19:41:57.000Z"},{"id":1126,"userId":101543,"uploadTime":"2020-09-09T21:35:37.000Z"},{"id":1127,"userId":101543,"uploadTime":"2020-09-09T21:41:41.000Z"},{"id":1128,"userId":101543,"uploadTime":"2020-09-09T21:48:46.000Z"},{"id":1132,"userId":101543,"uploadTime":"2020-09-14T16:17:27.000Z"},{"id":1082,"userId":101577,"uploadTime":"2020-08-21T15:24:35.000Z"},{"id":851,"userId":101589,"uploadTime":"2020-08-04T17:57:58.000Z"},{"id":852,"userId":101589,"uploadTime":"2020-08-04T18:05:07.000Z"},{"id":863,"userId":101589,"uploadTime":"2020-08-05T14:42:14.000Z"},{"id":864,"userId":101589,"uploadTime":"2020-08-05T15:23:50.000Z"},{"id":996,"userId":101589,"uploadTime":"2020-08-11T15:20:11.000Z"},{"id":1036,"userId":101589,"uploadTime":"2020-08-13T00:09:43.000Z"},{"id":1040,"userId":101589,"uploadTime":"2020-08-14T15:04:01.000Z"},{"id":1049,"userId":101589,"uploadTime":"2020-08-15T23:31:42.000Z"},{"id":1034,"userId":101612,"uploadTime":"2020-08-12T17:33:26.000Z"},{"id":889,"userId":101626,"uploadTime":"2020-08-06T14:47:18.000Z"},{"id":1130,"userId":101697,"uploadTime":"2020-09-14T16:09:16.000Z"},{"id":1131,"userId":101697,"uploadTime":"2020-09-14T16:09:49.000Z"},{"id":1133,"userId":101697,"uploadTime":"2020-09-14T16:19:16.000Z"}];var last24hoursByUserId =&nbsp;&nbsp; uploads&nbsp; .filter(upload=>&nbsp; &nbsp; new Date() - new Date(upload.uploadTime)&nbsp;&nbsp; &nbsp; <&nbsp; &nbsp; 24*60*60*1000&nbsp; )&nbsp; .sort((a,b)=>&nbsp; &nbsp; a.userId+a.uploadTime<b.userId+b.uploadTime?-1:1&nbsp; );console.log(last24hoursByUserId);.as-console-wrapper { max-height: 100% !important; top: 0; }

慕村9548890

这个答案首先解析字符串uploadTime。然后我检查是否uploadTime在之后yesterday并将其存储为isWithin24Hours. 我将此数据保存在一个单独的对象中sortData,以免污染或改变testArray.const testArray = [&nbsp; &nbsp; { id: 1133, userId: 101697, uploadTime: '2020-09-14T16:19:16.000Z' },&nbsp; &nbsp; { id: 1132, userId: 101543, uploadTime: '2020-09-14T16:17:27.000Z' },&nbsp; &nbsp; { id: 1131, userId: 101697, uploadTime: '2020-09-14T16:09:49.000Z' },&nbsp; &nbsp; { id: 1130, userId: 101697, uploadTime: '2020-09-14T16:09:16.000Z' },&nbsp; &nbsp; { id: 1128, userId: 101543, uploadTime: '2020-09-09T21:48:46.000Z' },&nbsp; &nbsp; { id: 1127, userId: 101543, uploadTime: '2020-09-09T21:41:41.000Z' },&nbsp; &nbsp; { id: 1126, userId: 101543, uploadTime: '2020-09-09T21:35:37.000Z' },&nbsp; &nbsp; { id: 1125, userId: 101543, uploadTime: '2020-09-09T19:41:57.000Z' },&nbsp; &nbsp; { id: 1082, userId: 101577, uploadTime: '2020-08-21T15:24:35.000Z' },&nbsp; &nbsp; { id: 1049, userId: 101589, uploadTime: '2020-08-15T23:31:42.000Z' },&nbsp; &nbsp; { id: 1040, userId: 101589, uploadTime: '2020-08-14T15:04:01.000Z' },&nbsp; &nbsp; { id: 1036, userId: 101589, uploadTime: '2020-08-13T00:09:43.000Z' },&nbsp; &nbsp; { id: 1035, userId: 101527, uploadTime: '2020-08-12T20:07:34.000Z' },&nbsp; &nbsp; { id: 1034, userId: 101612, uploadTime: '2020-08-12T17:33:26.000Z' },&nbsp; &nbsp; { id: 996, userId: 101589, uploadTime: '2020-08-11T15:20:11.000Z' },&nbsp; &nbsp; { id: 889, userId: 101626, uploadTime: '2020-08-06T14:47:18.000Z' },&nbsp; &nbsp; { id: 864, userId: 101589, uploadTime: '2020-08-05T15:23:50.000Z' },&nbsp; &nbsp; { id: 863, userId: 101589, uploadTime: '2020-08-05T14:42:14.000Z' },&nbsp; &nbsp; { id: 852, userId: 101589, uploadTime: '2020-08-04T18:05:07.000Z' },&nbsp; &nbsp; { id: 851, userId: 101589, uploadTime: '2020-08-04T17:57:58.000Z' }];// use timestamp to produces the same output for future readersconst now = 1600122461595 // Date.now();&nbsp;const yesterday = now - (24 * 60 * 60 * 1000);// create complementary data needed for sortconst sortData&nbsp; = new Map(testArray.map(({id, uploadTime}) => {&nbsp; uploadTime = Date.parse(uploadTime);&nbsp; return [id, { uploadTime, isWithin24Hours: uploadTime > yesterday }];}));testArray.sort((a, b) => {&nbsp; const _a = sortData.get(a.id), _b = sortData.get(b.id);&nbsp; return _b.isWithin24Hours - _a.isWithin24Hours&nbsp; &nbsp;// isWithin24Hours DESC&nbsp; &nbsp; &nbsp; || _a.isWithin24Hours && b.userId - a.userId // userId DESC if isWithin24Hours = 1&nbsp; &nbsp; &nbsp; || _b.uploadTime - _a.uploadTime;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // uploadTime DESC});console.log(testArray);console.table(testArray); // check browser console for this output我使用_b.isWithin24Hours - _a.isWithin24Hours( isWithing24Hours DESC) 将 24 小时内的记录移到前面。_a.isWithin24Hours此检查用作 XOR,如果和的值_b.isWithin24Hours彼此不同,将返回。下一个检查是_a.isWithin24Hours && b.userId - a.userId。_a.isWithin24Hours如果两者都不在 24 小时内(由于上一次检查,两者相同),将继续进行下一次检查。但是,如果两者都在 24 小时内,则使用check b.userId - a.userId( )。userId DESC如果两者都在 24 小时内并且具有相同的,或者两者都不在 24 小时内,则使用最后一次检查_b.uploadTime - _a.uploadTime( )。uploadTime DESCuserId
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript