简体   繁体   English

当索引是 JavaScript 中的键时,对 object 的嵌套数组进行排序

[英]Sort a nested array of object when index is a key in JavaScript

I am trying to sort nested array of objects, based off the key hours.我正在尝试根据关键时间对嵌套的对象数组进行排序。 The reason I am setting the index as userId, is that I want to be able to do highScoreList[userId] at a later time to grab the selected user information.我将索引设置为 userId 的原因是我希望稍后能够执行 highScoreList[userId] 来获取所选用户信息。

[
 '587665723162626': { userId: '587665723162626', hours: 0, lastHours: 0 },
 '120156769943556': { userId: '120156769943556', hours: 0, lastHours: 0 },
 '773193626386432': { userId: '773193626386432', hours: 10, lastHours: 2 }
]
    let highScoreList = [];
    //data is inserted via another function from mongoDB and below is how I inserted into the array.
    const updateHighScoreCache = (userId, hours, lastHours) =>{
        highScoreList[userId] = { userId, hours, lastHours };
    }
            
    function compare(a, b) {
        if(a.hours > b.hours) return 1;
        if(b.hours > a.hours) return -1;
        return 0;
    }
    highScoreList.sort(compare)

I have tried changing the if statement in the compare function to the following and the array did not change at all still:我尝试将比较 function 中的 if 语句更改为以下内容,但数组仍然没有改变:

   if(highScoreList[a].hours > highScoreList[b].hours) return 1;
   if(highScoreList[b].hours > highScoreList[a].hours) return -1;

The expected result after sort is:排序后的预期结果是:

[
 '773193626386432': { userId: '773193626386432', hours: 10, lastHours: 2 },
 '587665723162626': { userId: '587665723162626', hours: 0, lastHours: 0 },
 '120156769943556': { userId: '120156769943556', hours: 0, lastHours: 0 }
]

But I keep getting the original array.但我不断得到原始数组。

Your array literal at the top is not valid JS syntax.顶部的数组文字不是有效的 JS 语法。 The code shows where you go wrong.该代码显示您 go 错误的地方。

First you define highScoreList as an array, but then you don't populate the array, but create object properties for it, with:首先,您将highScoreList定义为一个数组,然后您不填充该数组,而是为其创建 object 属性,其中:

highScoreList[userId] = { userId, hours, lastHours };

These (non-index) properties are ignored by the typical array methods such as sort .这些(非索引)属性被典型的数组方法(例如sort )忽略。 Those methods work on the values stored at the array indexes .这些方法适用于存储在数组索引处的值。

So change this:所以改变这个:

highScoreList[userId] = { userId, hours, lastHours };

to this:对此:

highScoreList.push({ userId, hours, lastHours });

If however, the structure is returned to you as a plain object (not an array), like so:但是,如果该结构作为普通 object(不是数组)返回给您,如下所示:

{
  '587665723162626': { userId: '587665723162626', hours: 0, lastHours: 0 },
  '120156769943556': { userId: '120156769943556', hours: 0, lastHours: 0 },
  '773193626386432': { userId: '773193626386432', hours: 10, lastHours: 2 }
}

..then note that plain objects are not really the right tool for sorting . ..然后请注意,普通对象并不是真正的排序工具。 They better serve use cases where you don't care about order.它们更好地服务于您不关心订单的用例。 So if you really get an object like this (not an array) and need order, then covert that object to an array first, with:因此,如果您真的得到这样的 object(不是数组)并且需要订购,那么首先将 object 转换为数组,其中:

highScoreList = Object.values(highScoreList);

...and then call .sort ...然后调用.sort

Another remark: your compare function will do the job, but the way it really is done, is as follows:另一句话:您的compare function 将完成这项工作,但它真正完成的方式如下:

const compare = (a, b) => a.hours - b.hours;

If you array is:如果你的数组是:

const highScoreList = [
 { userId: '587665723162626', hours: 0, lastHours: 0 },
 { userId: '120156769943556', hours: 0, lastHours: 0 },
 { userId: '773193626386432', hours: 10, lastHours: 2 }
];

Then this code sorts it DESC:然后这段代码对它进行 DESC 排序:

highScoreList.sort((x,y) => y.hours - x.hours)

Then this code sorts it ASC:然后此代码对其进行 ASC 排序:

highScoreList.sort((x,y) => x.hours - y.hours)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM