Given the following object:
const data = {
"root": {
"title": "Shapes",
"shapes": { ... },
"description": "All sorts of geometrical whizz"
}
}
I'd need to find a way to sort this object to have the keys with objects appear above the keys with strings, such as below:
const data = {
"root": {
"shapes": { ... },
"title": "Shapes",
"description": "All sorts of geometrical whizz"
}
}
What I have tried was the sort() function:
const sorted = Object.values(root).sort((v) => typeof v === 'object' ? -1 : 1)
While this works to sort the values while keeping the root object pristine, the keys are lost in this new sorted array...
[
{...},
"Shapes",
"All sorts of geometrical whizz"
]
...while it must be kept as an object as the keys are used by a recursive Tree Component, where sorting like this is moot:
<ng-template *ngFor="let key of Object.keys(data)">
<div *ngIf="typeof key === 'object'>
<button (click)="show(value)">
{{key}}
</button>
<this-template [data]="data[key]">
</div>
<div *ngIf="typeof key === 'string'>
<button (click)="show(value)">
{{key}}
</button>
</div>
</ng-template>
as the component will still render based on the structure of root:
↓ root
|- title
|- > shapes
|- description
while the desired structure is this:
↓ root
|- > shapes
|- title
|- description
Thus, to define the problem in a single question;
How would I be able to sort an object by the type of the key's value, in order to render a recursive component to display items with objects before items with strings?