我有一个对象数组。我需要使用两个条件进行分类。在两个条件下排序的对象数组
[{
id: 412,
start_date: 1488479400,
status: 1
}, {
id: 560,
start_date: 1499451100,
status: 0
}, {
id: 112,
start_date: 1499091200,
status: 0
}, {
id: 512,
start_date: 1488474500,
status: 1
}, {
id: 750,
start_date: 1483473100,
status: 1
}, {
id: 123,
start_date: 1499106600,
status: 0
}, ]
我需要使用两个条件对此进行排序。
- 所有与状态1的对象应该先
- 日期应该是在第一降序排列,即最高的日期。
这里的预期输出
[{
id: 750,
start_date: 1483473100,
status: 1
}, {
id: 512,
start_date: 1488474500,
status: 1
}, {
id: 412,
start_date: 1488479400,
status: 1
}, {
id: 112,
start_date: 1499091200,
status: 0
}, {
id: 123,
start_date: 1499106600,
status: 0
}, {
id: 560,
start_date: 1499451100,
status: 0
}, ]
我试过之后this answer
数组分配给数据,然后
data.sort(function(a,b){return a.start_date - b.start_date()});
但它没有那种使用start_date
'b.start_date()'这不是一个功能。 –
在你的小提琴中日期的格式不一样(事实上,在你的小提琴中它可能没有做你期望的,因为'01-01-2017' *没有*引号是数字-2017(1减1减2017) 。你的真实数据是否有你的问题中显示的'start_date'? – nnnnnn