【发布时间】:2023-02-21 23:22:54
【问题描述】:
我有一个对象数组:
const arr = [
{
name: Exhibit A
},
{
name: Exhibit A1
},
{
name: Exhibit A2
},
{
name: Exhibit B
},
{
name: Exhibit C
},
{
name: Exhibit C1
},
{
name: Exhibit C2
},
{
name: Exhibit C3
},
]
我需要对它进行反向排序,但没有编号的展品应该首先在其组中。
像那样:
const result = [
{
name: Exhibit C
},
{
name: Exhibit C3
},
},
{
name: Exhibit C2
},
},
{
name: Exhibit C1
},
{
name: Exhibit B
},
{
name: Exhibit A
},
{
name: Exhibit A2
},
{
name: Exhibit A1
}
]
const result = [...arr].sort((a, b) => {???})
简单的 localeCompare 没有像我想要的那样工作,所以我需要找到另一种方法。
有任何想法吗?
【问题讨论】:
-
你试过什么了?
标签: javascript arrays sorting