var array = [
{
"checked": true,
"name":"Name",
"type": {
"id": 1,
"tag": "tag"
}
},
{
"checked": true,
"name":"Name",
"type": {
"id": 3,
"tag": "tag"
}
},
{
"checked": false,
"name":"Name",
"type": {
"id": 2,
"tag": "tag"
}
},
];
I want to sort the array by checked and type.id. Im using the following sorting code but it seems to have trouble with "type.id" since my list is not grouped by those if checked are.
sortByPriority(array, ['checked', 'type.id']);
sortByPriority(data, priorities) {
if (priorities.length == 0 || data.length == 0) {
return data;
}
const nextPriority = priorities[0];
const remainingPriorities = priorities.slice(1);
const matched = data.filter(item => item.hasOwnProperty(nextPriority));
const remainingData = data.filter(item => !item.hasOwnProperty(nextPriority));
return this.sortByPriority(matched, remainingPriorities)
.sort((a, b) => (a[nextPriority] > b[nextPriority]) ? 1 : -1)
.concat(this.sortByPriority(remainingData, remainingPriorities));
}
Any ideas on how to sort on the type object as well?
(I haven't been able to find another question with an answer with a generic sorter able to sort based on objects in the array)