Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 按不同键对已排序数组重新排序_Javascript_Arrays_Sorting - Fatal编程技术网

Javascript 按不同键对已排序数组重新排序

Javascript 按不同键对已排序数组重新排序,javascript,arrays,sorting,Javascript,Arrays,Sorting,我有这个数组,它已经按上传时间排序了 [ { 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: 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' }
    ]

现在我想使用它,但仅适用于具有相同用户ID的对象,并且上载时间距离当前时间不到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' },
      { 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' }
    ]

编辑:我最终是这样做的,比其他方法长一点:

    let dict = {}
    let dictArr = []
    let resultGroup = []
    let resultNoGroup = []
    
    temp.forEach((item) => {
        if (!dict[item.userId]) {
            dict[item.userId] = []
        }
        dict[item.userId].push(item)
        dict[item.userId].sort((a, b) => a.uploadTime - b.uploadTime)
    })
    
    for (let key in dict) {
        dictArr.push(dict[key])
    }
    
    dictArr.sort((a, b) => (a[0].uploadTime > b[0].uploadTime ? -1 : 1))
    
    for (let i = 0; i < dictArr.length; i++) {
        for (let j = 0; j < dictArr[i].length; j++) {
            const timeDiff = Math.abs(
                Math.round(
                    (new Date().getTime() -
                        new Date(dictArr[i][j].uploadTime).getTime()) /
                        (1000 * 3600)
                )
            )
            if (timeDiff <= 24) {
                resultGroup.push(dictArr[i][j])
            } else {
                resultNoGroup.push(dictArr[i][j])
            }
        }
    }
    
    resultNoGroup.sort((a, b) => (a.uploadTime > b.uploadTime ? -1 : 1))
    
    const result = [...resultGroup, ...resultNoGroup]
    
    console.log(result)
让dict={}
让dictArr=[]
让resultGroup=[]
让resultNoGroup=[]
每小时温度((项目)=>{
如果(!dict[item.userId]){
dict[item.userId]=[]
}
dict[item.userId].push(项目)
dict[item.userId].sort((a,b)=>a.uploadTime-b.uploadTime)
})
for(让我们输入dict){
dictArr.push(dict[按键])
}
dictArr.sort((a,b)=>(a[0]。上载时间>b[0]。上载时间?-1:1))
for(设i=0;ib.上传时间?-1:1))
常量结果=[…结果组,…结果组]
console.log(结果)
这对我来说是意料之中的事!

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);


 
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-091:41:000Z”,userId:“uploadTime”:“uploadTime”{“2020-09-09T21:48:46.000Z”{“id”:1132,“用户id”:101543,“上传时间”:“2020-09-14T16:17:27.000Z”},“id”:1082,“用户id”:101577,“上传时间”:“2020-08-21T15:24:35.000Z”},“id”:851,“用户id”:101589,“上传时间”:“2020-08-04T17:57:58.000Z”},“id”:852,“用户id”:101589,“上传时间”:“2020-08-T18:01000z”{“上传时间”:“用户id”:“上传时间”:2020-08-05T14:42:14.000Z“},{“id”:864,“用户id”:101589,“上传时间”:“2020-08-05T15:23:50.000Z”},{“id”:996,“用户id”:101589,“上传时间”:“2020-08-11T15:20:11.000Z”},{“id”:1036,“用户id”:101589,“上传时间”:“2020-08-13T00:09:43.000Z},{“id”:1040,“用户id”:101589,“上传时间”:“2020-08-14T15:1049”,用户id:“上传时间”:“上传时间”:2020-08-15T23:31:42.000Z“},{“id”:1034,“用户id”:101612,“上载时间”:“2020-08-12T17:33:26.000Z”},{“id”:889,“用户id”:101626,“上载时间”:“2020-08-06T14:47:18.000Z”},{“id”:1130,“用户id”:101697,“上载时间”:“2020-09-14T16:09:16.000Z},{“id”:1131,“用户id”:101697,“上载时间”:“2020-08-06T16:1499”userId:“上载时间”:“上载时间”:2020-09-14T16:19:16.000Z“}];
var last24hoursByUserId=
上传
.filter(上传=>
新日期()-新日期(upload.uploadTime)
<
24*60*60*1000
)
.排序((a,b)=>
a、 userId+a.uploadTime
现在我想使用它,但仅适用于具有相同用户ID的对象,并且上载时间距离当前时间不到24小时

这一次需要稍微更改排序(请参见代码中的注释):

如果两个日期都少于24小时,请在用户ID上排序,否则继续上载时间以保留顺序

var数据=[
{id:1133,userId:101697,上传时间:'2020-09-14T16:19:16.000Z'},
{id:1132,userId:101543,上传时间:'2020-09-14T16:17:27.000Z'},
{id:1131,userId:101697,上传时间:'2020-09-14T16:09:49.000Z'},
{id:1130,userId:101697,上传时间:'2020-09-14T16:09:16.000Z'},
{id:1128,userId:101543,上传时间:'2020-09-09T21:48:46.000Z'},
{id:1127,userId:101543,上传时间:'2020-09-09T21:41:41.000Z'},
{id:1126,userId:101543,上传时间:'2020-09-09T21:35:37.000Z'},
{id:1125,userId:101543,上传时间:'2020-09-09T19:41:57.000Z'},
{id:1082,userId:101577,上传时间:'2020-08-21T15:24:35.000Z'},
{id:1049,userId:101589,上传时间:'2020-08-15T23:31:42.000Z'},
{id:1040,userId:101589,上传时间:'2020-08-14T15:04:01.000Z'},
{id:1036,userId:101589,上传时间:'2020-08-13T00:09:43.000Z'},
{id:1035,userId:101527,上传时间:'2020-08-12T20:07:34.000Z'},
{id:1034,userId:101612,上传时间:'2020-08-12T17:33:26.000Z'},
{id:996,userId:101589,上传时间:'2020-08-11T15:20:11.000Z'},
{id:889,userId:101626,上传时间:'2020-08-06T14:47:18.000Z'},
{id:864,userId:101589,上传时间:'2020-08-05T15:23:50.000Z'},
{id:863,userId:101589,上传时间:'2020-08-05T14:42:14.000Z'},
{id:852,userId:101589,上传时间:'2020-08-04T18:05:07.000Z'},
{id:851,userId:101589,上传时间:'2020-08-04T17:57:58.000Z'}
];
数据排序(函数(a,b){
//计算与当前日期的差异
var dcta=(new Date()).getTime()-(new Date(a.uploadTime)).getTime();
var dctb=(new Date()).getTime()-(new Date(b.uploadTime)).getTime();
//然后在几个小时内转换
风险值h1=((dctb)/1000)/60)/60;
var h2=((dctb/1000)/60)/60;
//如果两者都少于24小时。。。。

if(h1这个答案首先解析
uploadTime
字符串。然后我检查
uploadTime
是否在
昨天
之后,并在24小时内将其存储为
iswithin24小时
。我将此数据保存在一个单独的对象
sortData
中,以避免污染或变异
testArray
中的对象

const testArray=[
{id:1133,userId:101697,上传时间:'2020-09-14T16:19:16.000Z'},
{id:1132,userId:101543,上传时间:'2020-09-14T16:17:27.000Z'},
{id:1131,userId:101697,上传时间:'2020-09-14T16:09:49.000Z'},
{id:1130,userId:101697,上传时间:'2020-09-14T16:09:16.000Z'},
{id:1128,userId:101543,上传时间:'2020-09-09T21:48:46.000Z'},
{id:1127,userId:101543,上传时间:'2020-09-09T21:41:41.000Z'},
{id:1126,userId:101543,上传时间:'2020-09-09T21:35:37
[ { 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' } ]