根据多个值对数组排序

时间:2020-03-17 11:44:02

标签: javascript

我正在尝试根据多个值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,但到目前为止没有运气。

5 个答案:

答案 0 :(得分:2)

您可以使用sort方法,并使用普通的javascript。

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"}]

arr.sort((a, b) => b.report_yr - a.report_yr || b.report_qtr - a.report_qtr)
console.log(arr)

答案 1 :(得分:0)

这可能对您有用。

data.sort(function (x, y) {
    var n = y.report_qtr - x.report_qtr;
    if (n !== 0) {
        return n;
    }

    return y.report_yr- x.report_yr;
});

答案 2 :(得分:0)

使用sort方法

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'
}]

arr.sort((b, a) => +a.report_yr - +b.report_yr || +a.report_qtr - +b.report_qtr )

console.log(arr)

答案 3 :(得分:0)

如果您添加report_yr + report_qtr并按降序排序

    arr = arr.sort((a, b) => {
        const one = parseInt(a.report_yr) + parseInt(a.report_qtr);
        const two = parseInt(b.report_yr) + parseInt(b.report_qtr);
        if (one > two) {
            return -1
        }
        return 1;
    });

答案 4 :(得分:0)

使用_.orderBy代替_.sortBy。如果不这样做,就无法对降序进行排序。

这将对report_qtr降序和report_yr升序进行排序。我注意到您想要的输出与排序有矛盾……

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'
}];

let sortedArr = _.orderBy(arr, ['report_qtr', 'report_yr'], ['desc', 'asc']);

console.log(sortedArr);
.as-console-wrapper {
  top: 0;
  max-height: 100% !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>


如果您确实 想要该订单,则可以执行以下操作:

  1. 查找年份频率
  2. 按频率排序
  3. 按年份排序
  4. 检查年份是否为奇数,按四分之一降序排列
  5. 检查年份是否是偶数,按季度升序排序
  6. 回到按季度递增排序

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' }
];

const yearFreq = arr.reduce((o, i) => ({...o, [i.report_yr] : (o[i.report_yr] || 0) + 1}), {});

let sortedArray = arr.sort((a, b) => {
  let diff = yearFreq[b.report_yr] - yearFreq[a.report_yr];
  if (diff !== 0) return diff;
  let a_year = parseInt(a.report_yr, 10);
  let b_year = parseInt(b.report_yr, 10);
  diff = b_year - a_year;
  if (diff !== 0) return diff;
  let a_odd = a_year % 2 === 1;
  let b_odd = b_year % 2 === 1;
  let a_qtr = parseInt(a.report_qtr, 10);
  let b_qtr = parseInt(b.report_qtr, 10);
  if (a_odd && b_odd) return b_qtr - a_qtr;
  if (!a_odd && !b_odd) return a_qtr - b_qtr;
  return a_qtr - b_qtr;
});

console.log(sortedArray);
.as-console-wrapper { top: 0; max-height: 100% !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

<!-- Expected

[{
  "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"
}]

-->

相关问题