按字母字符串属性值排序对象数组
我想在打字稿中对一个看起来像这样的对象进行排序:
[
{
"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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它可以正常工作在TS游乐场上
问题必须在其他地方
It works fine on TS Playground
The Problem has to be somewhere else