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
a.equipmentStatus.localeCompare(b.equipmentStatus)looks like a typo. You meana.name.localeCompare(b.name), right? Then, you’re matchingAvailable,Allocated, etc., but your objects haveavailableandallocated; 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.