Currently, I am diving into TypeScript and JS but encountering various challenges. One particular problem that is causing me trouble involves handling an array of Application objects with a nested structure. Each Application object consists of Document Types, which in turn contain Documents with Metadata.
Essentially, each Application has an array of Document Type, each Document Type has an array of Document, and each Document includes Metadata information. All three types - Application, Document Type, Document - have a property named 'name' within them.
My goal is to sort the entire array of Application objects, along with their nested objects, based on the ascending order of the 'name' property for each object. Despite attempting multiple examples, my sorting function only organizes the root level objects (Application) without considering the nested objects.
I've made an attempt with this example:
var simplePropertyRetriever = function(obj:any) {
return obj.name;
};
function sortArrayByName(propertyRetriever, arr:Application[]) {
arr.sort(function (a, b) {
var valueA = propertyRetriever(a);
var valueB = propertyRetriever(b);
if (valueA < valueB) {
return -1;
} else if (valueA > valueB) {
return 1;
} else {
return 0;
}
});
};
I would appreciate any guidance on how to achieve this desired sorting methodology. Below is a sample JSON structure showcasing the objects in descending order for better comprehension:
[
{
"name": "Application 2",
"children": [
{
"name": "Operations Manual",
"children": [
{
"name": "2nd-opsManual-app1",
"metadata": {
"size": 56,
"fileExtension": "docx"
}
},
...
]
},
...
]
},
{
"name": "Application 1",
"children": [
{
"name": "User Manual",
"children": [
{
"name": "1st-userManual-app1",
"metadata": {
"size": 56,
"fileExtension": "pdf"
}
},
...
]
},
...
]
}
]