按不同的键对对象数组进行排序

时间:2018-02-06 13:59:03

标签: javascript arrays sorting ecmascript-6

希望你能帮助我解决这个问题,我很确定这很简单,但我觉得我在这里缺少一些基本概念。

我有一组像

这样的对象
[{
  "id":"123",
  "creationUser":"user1",
  "updateUser":null,
  "creationDate":1517495569000,
  "updateDate":null,
  "text":"Hello World"
},

{
  "id":"543",
  "creationUser":"user2",
  "updateUser":"user3",
  "creationDate":1517912985769,
  "updateDate":1517921704448,
  "text":"Hello people"
},

{
  "id":"847",
  "creationUser":"user 4",
  "updateUser":null,
  "creationDate":null,
  "updateDate":1517913015110,
  "text":"Text 1"
},

{
  "id":"344",
  "creationUser":"user 1",
  "updateUser":"central",
  "creationDate":1517912979283,
  "updateDate":1517923926834,
  "text":"Aloha!"
}]

正如您所看到的,有些对象没有更新,所以这些值设置为null,但其他值已经更新,所以我想要的是按创建日期排序该数组,除非它已被更新,这意味着updatedDate是比较此数组的关键值。

我试过了:

let comments = conversation.sort(
   (a,b) => {
      if (a.updateDate){
         return (a.creationDate - b.updateDate);
      } else {
        return (b.creationDate - a.updateDate);
      }
});

但显然它仅在比较非更新对象时才有效。我很确定我错过了一些东西,但我不确定,我还想过将数组拆分成更新的阵列和非更新的阵列,然后再对它进行处理,但这对我来说听起来有些笨拙

拜托,如果你能给我一个暗示,那就太好了!

非常感谢!

4 个答案:

答案 0 :(得分:2)

您可以使用logical OR ||并将其视为默认creationDate

var array = [{ id: "123", creationUser: "user1", updateUser: null, creationDate: 1517495569000, updateDate: null, text: "Hello World" }, { id: "543", creationUser: "user2", updateUser: "user3", creationDate: 1517912985769, updateDate: 1517921704448, text: "Hello people" }, { id: "847", creationUser: "user 4", updateUser: null, creationDate: null, updateDate: 1517913015110, text: "Text 1" }, { id: "344", creationUser: "user 1", updateUser: "central", creationDate: 1517912979283, updateDate: 1517923926834, text: "Aloha!" }];

array.sort((a, b) => (a.updateDate || a.creationDate) - (b.updateDate || b.creationDate));

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

这样就可以通过选择为每个对象定义的日期来使用定义的日期。

  var aDate = a.updateDate || a.creationDate
  var bDate = b.updateDate || b.creationDate
  return bDate - aDate

答案 2 :(得分:0)

首先评估updateDate属性是否存在,如果不存在,请使用creationDate属性:



var data =[{
  "id":"123",
  "creationUser":"user1",
  "updateUser":null,
  "creationDate":1517495569000,
  "updateDate":null,
  "text":"Hello World"
},

{
  "id":"543",
  "creationUser":"user2",
  "updateUser":"user3",
  "creationDate":1517912985769,
  "updateDate":1517921704448,
  "text":"Hello people"
},

{
  "id":"847",
  "creationUser":"user 4",
  "updateUser":null,
  "creationDate":null,
  "updateDate":1517913015110,
  "text":"Text 1"
},

{
  "id":"344",
  "creationUser":"user 1",
  "updateUser":"central",
  "creationDate":1517912979283,
  "updateDate":1517923926834,
  "text":"Aloha!"
}];

data.sort(function (a,b) {
   var aDate = a.updateDate?a.updateDate:a.creationDate;
   var bDate = b.updateDate?b.updateDate:b.creationDate;
   return aDate - bDate;
});
console.log(data)




答案 3 :(得分:0)

 ((a.updateDate || 0) - (b.updateDate || 0)) || a.creationDate - b.creationDate

如果两个updateDates都未设置,则按创建日期进行比较,否则updateSet首先出现。