I got an array of object
const users= [{
Location: {
label: "aa",
value: "aa"
},
Name: "test7"
id: "002"
},
Location: {
label: "aa",
value: "aa"
},
Name: "test4"
id: "003"
},
{
Location: {
label: "ss",
value: "ss"
},
Name: "test4"
id: "004"
}]
I want to sort the array by first using Location.lable then using Name (Ascending order)
Here is what I have already done and it doesn't work
const dynamicSort = property => {
var sortOrder = 1;
if (property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
return function(a, b) {
var result;
if (property === "deviceLocation")
result =
a[property].label < b[property].label
? -1
: a[property].label > b[property].label
? 1
: 0;
else
result =
a[property] < b[property] ? -1 : a[property] > b[property] ? 1 : 0;
return result * sortOrder;
};
};
users.sort(dynamicSort("deviceLocation"))
the result should be like this :
const users= [{
Location: {
label: "aa",
value: "aa"
},
Name: "test4"
id: "003"
},
Location: {
label: "aa",
value: "aa"
},
Name: "test7"
id: "002"
},
{
Location: {
label: "ss",
value: "ss"
},
Name: "test4"
id: "004"
}]
how to sort the array object first using Location.label and then with Name. i tried lodash _.groupBy and after that sort but it didn't work