I need help sorting a JSON items with Javascript based on another array, and then alphabetically for the rest of the items.
Here is the array of the desired order for sorting the JSON items:
var order = [3,9,50,7];
The JSON data has an "ID" key that I want to sort using the order array, and the remaining items should be sorted by the "Name" key.
This is the initial JSON data:
var data = [
{
"id": "9",
"title": "B"
},
{
"id": "63",
"title": "Z"
},
{
"id": "433",
"title": "D"
},
{
"id": "50",
"title": "A"
},
{
"id": "2",
"title": "G"
}
]
Here is how I would like the final result to look:
var data = [
{
"id": "9",
"title": "B"
},
{
"id": "50",
"title": "A"
},
{
"id": "433",
"title": "D"
},
{
"id": "2",
"title": "G"
},
{
"id": "63",
"title": "Z"
}
]