0

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). 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: "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.

    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.

What do I need to add to my existing function to achieve that?

Thanks for helping!

,Coen

1
  • a.equipmentStatus.localeCompare(b.equipmentStatus) looks like a typo. You mean a.name.localeCompare(b.name), right? Then, you’re matching Available, Allocated, etc., but your objects have available and allocated; that’s inconsistent, and as a result, the objects won’t be sorted correctly. Voting to close as off-topic because the problem is essentially caused by two typos. Commented Mar 7, 2021 at 23:37

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.