简体   繁体   English

根据两个值按字母顺序对数组中的对象进行排序

[英]Sort objects in array based on two values alphabetically

I have a table which can be sorted on certain values.我有一个可以按某些值排序的表。 Currently it is sorted on specific equipmentStatus (not alphabetically, but defined in the initialStatusSorting variable below).目前它是按特定设备状态排序的(不是按字母顺序,而是在下面的 initialStatusSorting 变量中定义)。 However I also need to sort the item name alphabetically if they have the same equipmentStatus value.但是,如果它们具有相同的设备状态值,我还需要按字母顺序对项目名称进行排序。 I give an example what I have now, as you see I want the name to be sorted when the equipmentStatus is the same我举一个我现在拥有的例子,如你所见,我希望在equipmentStatus状态相同时对name进行排序

[
   {
    equipmentStatus: "available",
    name: "ab pump"
   },
   {
    equipmentStatus: "available",
    name: "aa pump"
   },
   {
    equipmentStatus: "available",
    name: "zbe pump"
   },
   {
    equipmentStatus: "allocated",
    name: "tr pump"
   },
   {
    equipmentStatus: "allocated",
    name: "vx pump"
   },
   {
    equipmentStatus: "allocated",
    name: "re pump"
   },
   {
    equipmentStatus: "RequestMaintenance",
    name: "tt pump"
   },
   {
    equipmentStatus: "RequestMaintenance",
    name: "vd pump"
   },
   {
    equipmentStatus: "RequestMaintenance",
    name: "ph pump"
   },
]

Now I have the following function to sort the equipmentStatus based on my given specifications, however I need to still alphabetically order the objects with the same equipmentStatus on the name key.现在我有以下 function 来根据我给定的规范对设备状态进行排序,但是我仍然需要按字母顺序对name键上具有相同设备状态的对象进行排序。

    const initialStatusSorting = { "Available" : 1, "Allocated": 2, "RequestMaintenance": 3, "RequestRepair": 4 , "Pickup": 5, default: Infinity };
    this.fetchOverviewPumps(requiredIds).then(() => {
      this.overviewPumps.allPumps.sort(function (a, b) {
        return (initialStatusSorting[a.equipmentStatus] || initialStatusSorting.default) - (initialStatusSorting[b.equipmentStatus] || initialStatusSorting.default) || a.equipmentStatus.localeCompare(b.equipmentStatus);
      });
    })```

So to recap: I need to sort 2 key-values, the one on specific equipmentStatus already works with the sort functionality above but I can't get the additional alpabetical sorting working on objects with the same equipmentStatus value.回顾一下:我需要对 2 个键值进行排序,特定设备状态上的键值已经与上面的排序功能一起使用,但我无法对具有相同equipmentStatus状态值的对象进行额外的字母排序。

What do I need to add to my existing function to achieve that?我需要在现有的 function 中添加什么来实现这一点?

Thanks for helping!感谢您的帮助!

,Coen ,科恩

To also sort it according to the name alphabetically if the equipmentStatus is the same, you just need to change this line in your code:如果设备状态相同,也可以根据名称的字母顺序对其进行排序,您只需在代码中更改此行:

return (initialStatusSorting[a.equipmentStatus] || initialStatusSorting.default) - (initialStatusSorting[b.equipmentStatus] || initialStatusSorting.default) || a.equipmentStatus.localeCompare(b.equipmentStatus);

To this:对此:

if (a.equipmentStatus !== b.equipmentStatus)
  return (initialStatusSorting[a.equipmentStatus] || initialStatusSorting.default) - (initialStatusSorting[b.equipmentStatus] || initialStatusSorting.default) || a.equipmentStatus.localeCompare(b.equipmentStatus);
else
  return a.name.localeCompare(b.name);

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

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