【发布时间】:2019-11-28 06:38:24
【问题描述】:
我想根据两个属性对数组进行升序排序。
我有以下看起来像的数据数组
[
{
id: 1,
name: 'ABP',
code: 1460,
subCode: '0010'
},
{
id: 2,
name: 'GKY',
code: 1460,
subCode: '0030'
},
{
id: 3,
name: 'CPT',
code: 1410,
subCode: '0070'
},
{
id: 4,
name: 'KLB',
code: 1470,
subCode: '0050'
},
{
id: 5,
name: 'POL',
code: 1430,
subCode: '0050'
},
{
id: 6,
name: 'FVB',
code: 1410,
subCode: '0050'
},
]
我想把它排序
[
{
id: 6,
name: 'FVB',
code: 1410,
subCode: '0050'
},
{
id: 3,
name: 'CPT',
code: 1410,
subCode: '0070'
},
{
id: 5,
name: 'POL',
code: 1430,
subCode: '0050'
},
{
id: 1,
name: 'ABP',
code: 1460,
subCode: '0010'
},
{
id: 2,
name: 'GKY',
code: 1460,
subCode: '0030'
},
{
id: 4,
name: 'KLB',
code: 1470,
subCode: '0050'
},
]
我想根据code 属性对数组进行升序排序,如果多个项目存在相同的code,那么我想根据code 属性的subCode 对其进行排序。
我在这里面临的问题是,subCode 是字符串,code 是数字。
我尝试过使用array.sort,也尝试使用整数解析subCode,但它返回了我不理解的不同数字。
【问题讨论】:
-
您用来解析子代码的代码在哪里,因为有不同的方式将字符串解析为数字...请粘贴该代码
-
到目前为止你编写了哪些代码来实现这一目标?
-
这可能就是你要找的东西:stackoverflow.com/q/6129952/12407377
-
Try this 但使用 underscore.js - 我认为你会喜欢的 javascript 库。
-
@SriVenkataPavanKumarMHS 我尝试使用 parseInt('0030') 返回 30。不知道为什么
标签: javascript arrays sorting