I have an array of object like below:
var data = [
{
name": "Name2",
"webOrderingEnabled": true,
"distance": 1.6989125091571928
},
{
"name": "Name3",
"webOrderingEnabled": false,
"distance": 1.9178283920396098
},
{
"name": "Name4",
"webOrderingEnabled": false,
"shutdown": {
"message": "",
"status": true
},
"distance": 6.94478210395609
},
{
"name": "Name1",
"webOrderingEnabled": true,
"shutdown": {
"message": "",
"status": false
},
"distance": 0.5368834377514055
}
]
I want to sort this array of object 1.webOrderingEnabled 2.shutdown.status = false 3. Distance The challenge is some object don't have shutdown key if it's not present consider to be store open i tried below approach it did't worked for me
data.sort((a, b) => {
if (a.shutdown?.status && b.shutdown?.starus || !a.shutdown?.status && !b.shutdown?.status &&
a.webOrderingEnabled || b.webOrderingEnabled) {
return a.distance - b.distance;
}
if (a.shutdown?.status) {
return -1;
}
return 1;
});
Expected:
[{
"name": "Name1",
"webOrderingEnabled": true,
"shutdown": {
"message": "",
"status": false
},
"distance": 0.5368834377514055
},
{
"name": "Name2",
"webOrderingEnabled": true
"distance": 1.6989125091571928
},
{
"name": "Name3",
"webOrderingEnabled": false,
"distance": 1.9178283920396098
},
{
"name": "Name4",
"webOrderingEnabled": false,
"shutdown": {
"message": "",
"status": true
},
"distance": 6.94478210395609
}]
CodePudding user response:
In general if you want to sort an array of objects according to fields A, B, C (in this order) you simply do:
if (a.A < b.A) return -1;
else if (a.A > b.A) return 1;
// otherwise a.A == b.A, so let's proceed with considering B now...
if (a.B < b.B) return -1;
else if (a.B > b.B) return 1;
// otherwise a.A == b.A && a.B == b.B, so let's proceed with considering C now...
if (a.C < b.C) return -1;
else if (a.C > b.C) return 1;
return 0;
Here you can think of a.A < b.A
as pseudocode for comparing that single attribute value, exactly how that is done depends on the types.
So your code should look something like
// I assume by "consider to be store open" you mean shutdown.status === false
function getShutdownStatus(a) {
return a?.shutdown?.status || false;
}
data.sort((a, b) => {
// Use ! here because true > false but it seems you want true to come first
if (!a.webOrderingEnabled < !b.webOrderingEnabled) return -1;
else if (!a.webOrderingEnabled > !b.webOrderingEnabled) return 1;
const aShutdown = getShutdownStatus(a);
const bShutdown = getShutdownStatus(b);
if (aShutdown < bShutdown) return -1;
else if (aShutdown > bShutdown) return 1;
// otherwise compare by distance
if (a.distance < b.distance) return -1;
else if (a.distance > b.distance) return 1
return 0;
});
Once you have this working you can simplify the code:
data.sort((a, b) => {
if (a.webOrderingEnabled != b.webOrderingEnabled) return b.webOrderingEnabled - a.webOrderingEnabled;
const aShutdown = getShutdownStatus(a);
const bShutdown = getShutdownStatus(b);
if (aShutdown != bShutdown) return aShutdown - bShutdown;
return a.distance - b.distance;
});