Currently, I am facing an issue with sorting an array of objects in TypeScript. The structure of my array is as follows:
[
{
"title": "Picture3.jpg",
"targetRange": "B2",
"type": "Bitmap"
},
{
"title": "Picture2.jpg",
"targetRange": "A2",
"type": "Bitmap"
},
{
"title": "Picture1.jpg",
"targetRange": "A1",
"type": "Bitmap"
}
]
My aim is to sort these objects based on the targetRange property in the order of A1, A2, B2.
I have already looked into a solution provided in this reference link, but it did not work for me.
Can someone guide me on how to correctly sort them by the value of targetRange using TypeScript?
I have attempted the following methods without success:
this.objs.sort((a, b) => a.targetRange!.localeCompare(b.targetRange!));
and
this.objs.sort((a,b) => (a.targetRange! < b.targetRange!) ? 1 : ((b.targetRange! > a.targetRange!) ? -1 : 0))