合并两个数组并按dateTime字段对其进行排序

时间:2019-01-15 04:24:24

标签: javascript arrays

我有两个排序数组,并将它们组合到一个新的listC中。

listA = [
  {id:"1234435", name:"apple", dateTime_ISO:"2019-01-15 17:27:30"},
  {id:"1234435", name:"orange", dateTime_ISO:"2019-01-15 10:25:30"},
  {id:"1234435", name:"banana", dateTime_ISO:"2019-01-15 10:25:02"},
  {id:"1234435", name:"pear", dateTime_ISO:"2019-01-15 07:21:52"},
  {id:"1234435", name:"lemon", dateTime_ISO:"2019-01-15 07:22:24"},
]

listB = [
  {id:"1234435", name:"bread", dateTime:"2019-01-15 17:27:34"},
  {id:"1234435", name:"rice", dateTime:"2019-01-15 09:25:30"},
  {id:"1234435", name:"noodle", dateTime:"2019-01-15 07:25:02"},
  {id:"1234435", name:"pie", dateTime:"2019-01-15 07:06:52"},
  {id:"1234435", name:"cake", dateTime:"2019-01-15 06:22:24"},
]

listC = this.listA.concat(this.listB)

如何基于dateTime对listC进行排序?

所以我在想获取一个仅包含dateTime的新dateTimeList,然后对该列表进行排序,并以某种方式对listC进行排序

dateTimeList = this.listA
                .map(x => x.dateTime_ISO)
                .concat(this.listB.map(x => x.dateTime));

但是有什么健壮的方法可以对此dateTimeList进行排序?

如果仅由于两个列表具有不同的字段名称(dateTime_ISO和dateTime)而无法完成操作,则假装它们是相同的。我可以将它们修改为与数据库中的相同。

感谢您的帮助。

4 个答案:

答案 0 :(得分:2)

您可以利用短路来对组合列表进行排序,同时仍然具有不同的属性。例如:

let listA = [{id:"1234435", name:"apple", dateTime_ISO:"2019-01-15 17:27:30"},{id:"1234435", name:"orange", dateTime_ISO:"2019-01-15 10:25:30"},{id:"1234435", name:"banana", dateTime_ISO:"2019-01-15 10:25:02"},{id:"1234435", name:"pear", dateTime_ISO:"2019-01-15 07:21:52"},{id:"1234435", name:"lemon", dateTime_ISO:"2019-01-15 07:22:24"},]
let listB = [{id:"1234435", name:"bread", dateTime:"2019-01-15 17:27:34"},{id:"1234435", name:"rice", dateTime:"2019-01-15 09:25:30"},{id:"1234435", name:"noodle", dateTime:"2019-01-15 07:25:02"},{id:"1234435", name:"pie", dateTime:"2019-01-15 07:06:52"},{id:"1234435", name:"cake", dateTime:"2019-01-15 06:22:24"},]
  
let listC = listA.concat(listB)

listC.sort((a, b) => {
    return (a.dateTime_ISO || a.dateTime).localeCompare((b.dateTime_ISO || b.dateTime))
})
console.log(listC)

答案 1 :(得分:1)

您可以使用sort(),并在排序条件中检查是否存在某些属性(例如:dateTime_ISO),如果不存在,请使用备用属性名称(dateTime):

const listA = [
  {id:"1234435", name:"apple", dateTime_ISO:"2019-01-15 17:27:30"},
  {id:"1234435", name:"orange", dateTime_ISO:"2019-01-15 10:25:30"},
  {id:"1234435", name:"banana", dateTime_ISO:"2019-01-15 10:25:02"},
  {id:"1234435", name:"pear", dateTime_ISO:"2019-01-15 07:21:52"},
  {id:"1234435", name:"lemon", dateTime_ISO:"2019-01-15 07:22:24"},
]

const listB = [
  {id:"1234435", name:"bread", dateTime:"2019-01-15 17:27:34"},
  {id:"1234435", name:"rice", dateTime:"2019-01-15 09:25:30"},
  {id:"1234435", name:"noodle", dateTime:"2019-01-15 07:25:02"},
  {id:"1234435", name:"pie", dateTime:"2019-01-15 07:06:52"},
  {id:"1234435", name:"cake", dateTime:"2019-01-15 06:22:24"},
]

let listC = listA.concat(listB);

// Now, lets sort.

let sortedListC = listC.sort((a, b) =>
{
    let x = (a.dateTime_ISO || a.dateTime);
    let y = (b.dateTime_ISO || b.dateTime);

    return ((x > y) && 1) || ((x < y) && -1) || 0;
});

console.log(sortedListC);

答案 2 :(得分:0)

由于listA和listB已经按降序排序,所以我认为您可以利用这一事实并将它们合并。这应该是O(n)-如果在串联后对结果进行排序,则应该是(nlog(n))。

尝试这样的事情

var listA = [
  {id:"1234435", name:"apple", dateTime_ISO:"2019-01-15 17:27:30"},
  {id:"1234435", name:"orange", dateTime_ISO:"2019-01-15 10:25:30"},
  {id:"1234435", name:"banana", dateTime_ISO:"2019-01-15 10:25:02"},
  {id:"1234435", name:"pear", dateTime_ISO:"2019-01-15 07:21:52"},
  {id:"1234435", name:"lemon", dateTime_ISO:"2019-01-15 07:22:24"},
];

var listB = [
  {id:"1234435", name:"bread", dateTime:"2019-01-15 17:27:34"},
  {id:"1234435", name:"rice", dateTime:"2019-01-15 09:25:30"},
  {id:"1234435", name:"noodle", dateTime:"2019-01-15 07:25:02"},
  {id:"1234435", name:"pie", dateTime:"2019-01-15 07:06:52"},
  {id:"1234435", name:"cake", dateTime:"2019-01-15 06:22:24"},
];

function merge(a, b) {
        var ret = [];
        while (a.length && b.length) {
                var smallest = a[0].dateTime_ISO > b[0].dateTime ? a.shift() : b.shift();
                ret.push(smallest);
        }
        return ret.concat(a.length ? a : b);
};

console.log(JSON.stringify(merge(listA, listB), null, 2));

答案 3 :(得分:-1)

dateTimeList = this.listA.sort(function(a,b){
      //return a.attributes - b.attributes;
      if(a.date < b.date)
          return 1;
      if(a.date > b.date)
          return -1;
    });

是按日期排序的好方法。