简体   繁体   English

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

[英]Sort an array of objects by different keys

Hope you can help me with this question I have, that I'm pretty sure it's simple but I feel I'm missing some basic concepts here.希望你能帮我解决这个问题,我很确定这很简单,但我觉得我在这里遗漏了一些基本概念。

I have an array of objects like我有一系列的对象,如

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

As you can see there are some objects that doesn't have been updated so those values are set to null, but others have been updated, so what I would like to do is to order that array by creation date unless it has been updated, which mean that the updatedDate is the key value to compare this array.如您所见,有些对象尚未更新,因此这些值设置为 null,但其他对象已更新,因此我想做的是按创建日期对该数组进行排序,除非它已更新,这意味着 updatedDate 是比较此数组的键值。

I have tried:我试过了:

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

But obviously it only works when comparing non updated objects.但显然它仅在比较未更新的对象时才有效。 I'm pretty sure I'm missing something but I'm not sure, I also thought on splitting the array into updated array and non updated array and then merege it, but it sounds a bit hacky to me.我很确定我遗漏了一些东西,但我不确定,我还想过将数组拆分为更新的数组和未更新的数组,然后合并它,但这对我来说听起来有点麻烦。

Please please, if you can give me a hint on this, it would be great!拜托了,如果你能给我一个提示,那就太好了!

Thanks a lot!非常感谢!

You could use logical OR ||您可以使用逻辑 OR || and take as default creationDate .并作为默认的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; }

So that make it so you use the defined dates by picking the one that is defined for each object.这样就可以通过选择为每个对象定义的日期来使用定义的日期。

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

Evaluate the existence of the updateDate property first and if not exists, use the creationDate property:首先评估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)

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

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

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

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