0

I want to sort an array of objects in TypeScript which looks like this:

[
    {
        "title": "Picture3.jpg",
        "targetRange": "B2",
        "type": "Bitmap"
    },
     {
        "title": "Picture2.jpg",
        "targetRange": "A2",
        "type": "Bitmap"
    },
    {
        "title": "Picture1.jpg",
        "targetRange": "A1",
        "type": "Bitmap"
    }
]

I want to have my objects sorted after the targetRange in following order: A1, A2, B2

I refered to this question, but it won't sort it in any way.

How can I sort them by the value of targetRange in TypeScript?

I tried the following things already:

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))```
Chris
  • 124
  • 9
  • https://stackoverflow.com/questions/4340227/sort-mixed-alpha-numeric-array – epascarello Jul 08 '22 at 13:11
  • Does this answer your question? [Sort mixed alpha/numeric array](https://stackoverflow.com/questions/4340227/sort-mixed-alpha-numeric-array) –  Jul 08 '22 at 13:30

1 Answers1

0

It works fine on TS Playground

The Problem has to be somewhere else

Filly
  • 411
  • 2
  • 8