按字母字符串属性值排序对象数组

发布于 2025-02-14 00:17:59 字数 859 浏览 1 评论 0原文

我想在打字稿中对一个看起来像这样的对象进行排序:

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

我想按以下顺序将我的对象分类:a1,a2,b2

我提到了this 问题,但它不会在任何中排序 方式。

如何按TypeScript中的Targetrange值对它们进行排序?

我已经尝试了以下内容

this.objs.sort((a, b) => a.targetRange!.localeCompare(b.targetRange!));

 this.objs.sort((a,b) => (a.targetRange! < b.targetRange!) ? 1 : ((b.targetRange! > a.targetRange!) ? -1 : 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))```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁许谁一生繁华 2025-02-21 00:18:00

它可以正常工作在TS游乐场上

问题必须在其他地方

It works fine on TS Playground

The Problem has to be somewhere else

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文