我将尝试根据多个值,即report_yr和report_qtr来对以下数组进行排序。该数组应首先按年份排序,然后按季度排序(对于同一年份)。
// 输入的数组
// 输入的数组
let arr = [{
report_yr: '2008',
report_qtr: '1'
},
{
report_yr: '2019',
report_qtr: '3'
},
{
report_yr: '2019',
report_qtr: '4'
},
{
report_yr: '2017',
report_qtr: '2'
},
{ report_yr: '2008',
report_qtr: '2'
}];
// 预期输出:
[
{ report_yr: '2019',
report_qtr: '4' },
{ report_yr: '2019',
report_qtr: '3' },
{ report_yr: '2008',
report_qtr: '1'
},{ report_yr: '2008',
report_qtr: '2'
},
{ report_yr: '2017',
report_qtr: '2' } ];
// 我的尝试:
我正在尝试使用loadash方法进行操作,
_.sortBy(arr,
['report_yr', 'report_qtr']);
同样尝试了orderBy,但迄今为止没有成功。
report_yr
是2018
时,你的结果随着report_qtr
上升,但当report_yr
是2019
时,对象降序? - Nick Parsons