I have an array of object like below:
var stores = [
{
"name": "store3",
"ditance": 8
},
{
"name": "Store5",
"distance": 7,
"web": {
"validateAttributes": {
"isSelectedDispostionSupported": true,
"isFutureOrderPossible": false
}
}
},
{
"name": "Store1",
"distance": 12,
"web": {
"validateAttributes": {
"isSelectedDispostionSupported": true,
"isOpen": true
}
}
},
{
"name": "store2",
"distance": 13,
"web": {
"validateAttributes": {
"isSelectedDispostionSupported": true,
"isOpen": true
}
}
}
]
Expected Result to be sort based on isopen and distance
[
{
"name": "Store1",
"distance": 12,
"web": {
"validateAttributes": {
"isSelectedDispostionSupported": true,
"isOpen": true
}
}
},
{
"name": "store2",
"distance": 13,
"web": {
"validateAttributes": {
"isSelectedDispostionSupported": true,
"isOpen": true
}
}
},
{
"name": "Store5",
"distance": 7,
"web": {
"validateAttributes": {
"isSelectedDispostionSupported": true,
"isFutureOrderPossible": false
}
}
},
{
"name": "store3",
"ditance": 8
}
]
Need to sort an array based on isOpen and distance .The challenge is some object have web property and some object don't have. Even if web available sometimes isOpen won't their. I have tried the below approch it's not working
const sorter = (a, b) => {
if (a.web) {
if (a.web.validateAttributes) {
if (a.web.validateAttributes.isOpen) {
return 1;
} else if (b.web.validateAttributes.isOpen) {
return -1;
} else {
return 1;
};
} else {
return 1;
}
} else {
return 1;
}
};
stores.sort(sorter);
Try this
const stores = [{"name":"store3","ditance":8},{"name":"Store5","distance":7,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isFutureOrderPossible":false}}},{"name":"Store1","distance":12,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}},{"name":"store2","distance":13,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}}]
const sorter = (a,b)=> {
if(!b.web) {
return -1;
}
else if(!b.web.validateAttributes) {
return -1;
}
else if(!b.web.validateAttributes.isOpen) {
return -1;
}
return (a.distance - b.distance)
}
console.log(stores.sort(sorter))
Try this.
const stores = [{"name":"store3","ditance":8},{"name":"Store5","distance":7,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isFutureOrderPossible":false}}},{"name":"Store1","distance":12,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}},{"name":"store2","distance":13,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}}]
const sorted = stores.slice();
sorted.sort((a, b) => {
if (!a.web || !b.web) {
return -1;
}
if (!a.web.validateAttributes || !b.web.validateAttributes) {
return -1;
}
const x = a.web.validateAttributes.isOpen || false;
const y = b.web.validateAttributes.isOpen || false;
return (x === y) ? a.distance - b.distance : -1;
})
console.log(sorted)
.as-console-wrapper { max-height: 100% !important; top: 0; }
I see 2 problems.
var stores = [{"name":"store3","distance":8},{"name":"Store5","distance":7,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isFutureOrderPossible":false}}},{"name":"Store1","distance":12,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}},{"name":"store2","distance":13,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}}];
stores.sort((a, b) => {
if (a.web?.validateAttributes?.isOpen && b.web?.validateAttributes?.isOpen || !a.web?.validateAttributes?.isOpen && !b.web?.validateAttributes?.isOpen) {
return a.distance - b.distance;
}
if (a.web?.validateAttributes?.isOpen) {
return -1;
}
return 1;
})
console.log(stores);