为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

叮当猫咪 2023-03-03 15:46:57
我有这个数组,已经按上传时间排序    [      { 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' },    ]
查看完整描述

4 回答

?
holdtom

TA贡献1805条经验 获得超10个赞

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


这次排序需要稍微改变一下(看代码中的注释):


如果两个日期都小于 24 小时,则按userId排序,否则继续使用 uploadTime 以保留顺序。


var data = [

    {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'}

];


data.sort(function (a, b) {

    // compute the difference with current date

    var dcta = (new Date()).getTime() - (new Date(a.uploadTime)).getTime();

    var dctb = (new Date()).getTime() - (new Date(b.uploadTime)).getTime();


   // convert then in hours

    var h1 = (((dctb) / 1000) / 60)/60;

    var h2 = ((dctb / 1000) / 60)/60;


    // if both are less than 24hours....    

    if (h1 <=24 && h2 <= 24) {

        return b.userId - a.userId;

    } else {

        return (new Date(b.uploadTime)).getTime() - (new Date(a.uploadTime)).getTime();

    }

});

console.log(JSON.stringify(data).replace(/\},/g, '},\n'));


查看完整回答
反对 回复 2023-03-03
?
小唯快跑啊

TA贡献1863条经验 获得超2个赞

let testArray = [

    { 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' }

];





testArray = testArray.sort((x, y) => {

    return (y.userId - x.userId);

});


testArray = testArray.filter(currentObj => {

    return (new Date() - new Date(currentObj.uploadTime)) < 24 * 60 * 60 * 1000;

})


    

    

    console.log(testArray);



 

我们排序比较对象的两个不同属性。现在您将按 userId 对用户进行排序,然后按从最新到最旧的上传时间排序。


结果:


[ { 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' } ]


查看完整回答
反对 回复 2023-03-03
?
哔哔one

TA贡献1854条经验 获得超8个赞

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 = 

  uploads

  .filter(upload=>

    new Date() - new Date(upload.uploadTime) 

    <

    24*60*60*1000

  )

  .sort((a,b)=>

    a.userId+a.uploadTime<b.userId+b.uploadTime?-1:1

  )

;


console.log(last24hoursByUserId);

.as-console-wrapper { max-height: 100% !important; top: 0; }


查看完整回答
反对 回复 2023-03-03
?
慕村9548890

TA贡献1884条经验 获得超4个赞

这个答案首先解析字符串uploadTime。然后我检查是否uploadTime在之后yesterday并将其存储为isWithin24Hours. 我将此数据保存在一个单独的对象中sortData,以免污染或改变testArray.


const testArray = [

    { 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' }

];


// use timestamp to produces the same output for future readers

const now = 1600122461595 // Date.now(); 

const yesterday = now - (24 * 60 * 60 * 1000);


// create complementary data needed for sort

const sortData  = new Map(testArray.map(({id, uploadTime}) => {

  uploadTime = Date.parse(uploadTime);

  return [id, { uploadTime, isWithin24Hours: uploadTime > yesterday }];

}));


testArray.sort((a, b) => {

  const _a = sortData.get(a.id), _b = sortData.get(b.id);

  return _b.isWithin24Hours - _a.isWithin24Hours   // isWithin24Hours DESC

      || _a.isWithin24Hours && b.userId - a.userId // userId DESC if isWithin24Hours = 1

      || _b.uploadTime - _a.uploadTime;            // 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


查看完整回答
反对 回复 2023-03-03
  • 4 回答
  • 0 关注
  • 155 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信