简体   繁体   English

如何按3个条件和条件将字段数组排序,空值始终排在最后

[英]How can I sort an array of objects by 3 fields with conditions and null values always come last

I have an array of objectds and need to sort by 3 date fields (date1, date2, date3) 我有一个objectds数组,需要按3个日期字段(date1,date2,date3)排序

  1. fist sort by date1 ascending 拳头按date1升序排序
  2. if there are more docs with same date1 sort by date3 ascending 如果还有更多具有相同date1的文档,则按date3升序排序
  3. (docs with null or undefined date1 must be last) (具有null或undefined date1的文档必须是最后一个)
  4. if date1 is undefined or null sort by date2 ascending, 如果date1未定义或按date2升序排序为null,
  5. if there are more docs with same date2 sort by date3 ascending 如果还有更多具有相同date2的文档,则按date3升序排序

Lets say I have this array: 可以说我有这个数组:


    [
        { date1: null, date2: '20120-01-08', date3: '20120-01-06' },
        { date1: null, date2: '20120-01-09', date3: '20120-01-05' },
        { date1: null, date2: '20120-01-08', date3: '20120-01-05' },
        { date1: '20120-01-05', date2: '20120-01-07', date3: '20120-01-01' },
        { date1: '20120-01-04', date2: '20120-01-07', date3: '20120-01-02' },
        { date1: '20120-01-04', date2: '20120-01-10', date3: '20120-01-01' }
    ]

i need to get this: 我需要得到这个:


    [
        { date1: '20120-01-04', date2: '20120-01-10', date3: '20120-01-01' },
        { date1: '20120-01-04', date2: '20120-01-07', date3: '20120-01-02' },
        { date1: '20120-01-05', date2: '20120-01-07', date3: '20120-01-01' },
        { date1: null, date2: '20120-01-08', date3: '20120-01-05' },
        { date1: null, date2: '20120-01-08', date3: '20120-01-06' },
        { date1: null, date2: '20120-01-09', date3: '20120-01-05' }
    ]

I tried using array.sort() and managed to solve the fitst 3 requirements: 我尝试使用array.sort()并设法解决了最合适的3个要求:

  1. fist sort by date1 ascending 拳头按date1升序排序
  2. if there are more docs with same date1 sort by date3 ascending 如果还有更多具有相同date1的文档,则按date3升序排序
  3. (docs with null or undefined date1 must be last) (具有null或undefined date1的文档必须是最后一个)

using this sort function: 使用以下排序功能:


    var array = [
      { date2: "20120-01-08", date3: "20120-01-06" },
      { date2: "20120-01-09", date3: "20120-01-05" },
      { date2: "20120-01-08", date3: "20120-01-05" },
      { date1: "20120-01-05", date2: "20120-01-07", date3: "20120-01-01" },
      { date1: "20120-01-04", date2: "20120-01-07", date3: "20120-01-02" },
      { date1: "20120-01-04", date2: "20120-01-10", date3: "20120-01-01" }
    ];

    var result = array.sort(function(obj1, obj2) {
      if (!obj1.date1 || !obj2.date1) {
        return -1;
      } else {
        if (obj1.date1 > obj2.date1) return 1;
        if (obj1.date1 < obj2.date1) return -1;
        if (obj1.data3 > obj2.data3) return 1;
        if (obj1.date3 < obj2.date3) return -1;
        return 0;
      }
    });

I get this: 我得到这个:


    0: {date1: "20120-01-04", date2: "20120-01-10", date3: "20120-01-01"}
    1: {date1: "20120-01-04", date2: "20120-01-07", date3: "20120-01-02"}
    2: {date1: "20120-01-05", date2: "20120-01-07", date3: "20120-01-01"}
    3: {date2: "20120-01-08", date3: "20120-01-05"}
    4: {date2: "20120-01-09", date3: "20120-01-05"}
    5: {date2: "20120-01-08", date3: "20120-01-06"}

I'm not sure how to solve those 2: 我不确定如何解决这些2:

  1. if date1 is undefined or null sort by date2 ascending, 如果date1未定义或按date2升序排序为null,
  2. if there are more docs with same date2 sort by date3 ascending 如果还有更多具有相同date2的文档,则按date3升序排序

the records with undefined date1 are not sorted... 未定义date1的记录未排序...

https://codepen.io/wyzix33/pen/agaKRp https://codepen.io/wyzix33/pen/agaKRp

Thanks 谢谢

You could sort by 你可以排序

  • undefined / null / falsy values of date1 and sort them to bottom, date1 undefined / null / falsy值进行排序,然后将其排序到底部,
  • date1 or if falsy date2 , date1或false date2
  • date3 . date3

 var data = [{ date1: null, date2: '20120-01-08', date3: '20120-01-06' }, { date1: null, date2: '20120-01-09', date3: '20120-01-05' }, { date1: null, date2: '20120-01-08', date3: '20120-01-05' }, { date1: '20120-01-05', date2: '20120-01-07', date3: '20120-01-01' }, { date1: '20120-01-04', date2: '20120-01-07', date3: '20120-01-02' }, { date1: '20120-01-04', date2: '20120-01-10', date3: '20120-01-01' }]; data.sort((a, b) => !a.date1 - !b.date1 || (a.date1 || a.date2).localeCompare(b.date1 || b.date2) || a.date3.localeCompare(b.date3) ); console.log(data); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

I used a little hacking, but unfortunately it took a long time to execute! To set the undefined and null to the largest date in limit, then computed value of per item and do compare! 我使用了一点技巧,但是不幸的是执行起来花了很长时间!要将undefined和null设置为限制中的最大日期,然后计算每个项目的值并进行比较!

var array = [{
    date1: null,
    date2: "20120-01-08",
    date3: "20120-01-06"
}, {
    date1: null,
    date2: "20120-01-08",
    date3: "20120-01-05"
}, {
    date1: null,
    date2: "20120-01-09",
    date3: "20120-01-05"
},
{
    date1: "20120-01-05",
    date2: "20120-01-07",
    date3: "20120-01-01"
}, {
    date1: "20120-01-04",
    date2: "20120-01-05",
    date3: "20120-01-02"
},
{
    date1: "20120-01-04",
    date2: "20120-01-07",
    date3: "20120-01-01"
}];

var result = array.sort(function(obj1, obj2) {

    var copyObj1 = {...obj1},copyObj2 ={...obj2};
    var largestDate = '20121-12-31'

    copyObj1.date1 = copyObj1.date1 || largestDate
    copyObj2.date1 = copyObj2.date1 || largestDate

    var value1 = Object.values(copyObj1).join('')
    var value2 = Object.values(copyObj2).join('')
    return value1.localeCompare(value2)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM