JavaScript排序对象中的对象数组中的对象中的字段

发布于 2025-02-12 10:13:08 字数 543 浏览 2 评论 0原文

我的javascript不太熟练,我需要根据描述对数组进行排序的问题。我看了几个主题,但我试图对其进行排序,但是到目前为止没有运气。

我有一系列看起来像这样的对象:

{
  category: null,
  code: "GGG",
  id: 3,
  description: "Gama"
},    
{
  category: null,
  code: "AAA",
  id: 1,
  description: "Alfa"
},
{
  category: null,
  code: "BBB",
  id: 2,
  description: "Beta"
}

此数组由一个函数返回:

return refort.data

因此,想根据上升顺序的描述对结果集进行排序,这意味着我的列表在返回后看起来像这样:“ alfa”,“ beta”,“ gama”。

我试图使用排序函数:

return result.data.sort();

但这对我没有帮助。

I'm not very skilled with JavaScript and I have an issue where I need to sort array based on description. I looked several topics and I have tried to sort it but so far no luck..

I have an array of objects that looks like this:

{
  category: null,
  code: "GGG",
  id: 3,
  description: "Gama"
},    
{
  category: null,
  code: "AAA",
  id: 1,
  description: "Alfa"
},
{
  category: null,
  code: "BBB",
  id: 2,
  description: "Beta"
}

And this array is returned by a function that says:

return result.data

So want to sort that result set based on description on ascending order, which means my list would look like this after the return: "Alfa", "Beta", "Gama".

I was trying to use sort function:

return result.data.sort();

But that didn't help me.

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

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

发布评论

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

评论(1

苦行僧 2025-02-19 10:13:08

您可以做:

result.data.sort((a, b) => a.description.localeCompare(b.description)));

You can do:

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