Here is my JSON array:
var jData = [
{id: 1, parent: null},
{id: 2, parent: null},
{id: 3, parent: 1},
{id: 4, parent: 2},
{id: 5, parent: 2},
{id: 6, parent: 1}];
I would like to sort it in the following order (first by id then by parent):
[
{id: 1, parent: null},
{id: 3, parent: 1},
{id: 6, parent: 1}
{id: 2, parent: null},
{id: 4, parent: 2},
{id: 5, parent: 2},
];
What is the most efficient way to achieve this in JavaScript?
I have attempted to use the following code, but with no success:
jData .sort((a, b) => a.id - b.id || a.parent - b.parent);
Please assist!