按两个条件排序数据?

时间:2018-04-09 23:01:11

标签: javascript arrays json

我有一个JSON对象,我试图按时间和用户名按字母顺序排序:

示例数据:

   const obj = [ {"timestamp": 1487184625, "user": "Eric", "action": "navigate"},
    {"timestamp": 1487184655, "user": "Bill", "action": "browse"},
    {"timestamp": 1487184685, "user": "Eric", "action": "key press"},
    {"timestamp": 1487184715, "user": "John", "action": "idle"},
    {"timestamp": 1487184755, "user": "Tran", "action": "search"},
    {"timestamp": 1487098049, "user": "Tran", "action": "click"},
    {"timestamp": 1487098079, "user": "Eric", "action": "click"},
    {"timestamp": 1487098109, "user": "Tran", "action": "click"},
    {"timestamp": 1487098139, "user": "Bill", "action": "navigate"},
    {"timestamp": 1487184625, "user": "Eric", "action": "navigate"},
    {"timestamp": 1487184655, "user": "Bill", "action": "browse"},
    {"timestamp": 1487184685, "user": "Eric", "action": "key press"},
    {"timestamp": 1487184715, "user": "John", "action": "idle"},
    {"timestamp": 1487184755, "user": "Tran", "action": "search"},
    {"timestamp": 1487098049, "user": "Tran", "action": "click"},
    {"timestamp": 1487098079, "user": "Eric", "action": "click"},
    {"timestamp": 1487098109, "user": "Tran", "action": "click"},
    {"timestamp": 1487098139, "user": "Bill", "action": "navigate"}]

我尝试过这样的事情:

console.log(obj.sort((a,b) => {
  // if(a.user.localeCompare(b.user)){
  //   return 1
  // }
  // if(!a.user.localeCompare(b.user)){
  //   return -1
  // }
    if(a.timestamp > b.timestamp){
      return -1
    }
    if(a.timestamp < b.timestamp){
      return 1
    }
}))

但我似乎无法让它发挥作用。我也想避免使用Lodash。有什么建议吗?

4 个答案:

答案 0 :(得分:2)

检查属性localeCompareuser的结果,如果它是-11,则返回该结果。否则(结果为0),返回timestamp

的比较
obj.sort((a,b) => {
    var userComparaison = a.user.localeCompare(b.user);
    if(userComparaison) {                                 // userComparaison is either -1 or 1 (both are thruthy values)
        return userComparaison;                           // return userComparaison as the sorting criteria
    } else {                                              // otherwise (userComparaison is 0 which is falsy)
        return b.timestamp - a.timestamp;                 // compare by timestamp
    }
}

使用逻辑||

可以更短
obj.sort((a,b) => {
    return a.user.localeCompare(b.user) || b.timestamp - a.timestamp;
}

如果localeCompare返回真值( -11),则会返回该真值,其余为{{1}将被忽略。如果它返回了一个假值( ||),那么将返回0的另一个操作数的值(按||排序)

答案 1 :(得分:2)

这可以通过单个排序功能线来解决:

 var results = obj.sort((a, b) => {
   return a.user.localeCompare(b.user) || b.timestamp - a.timestamp
 })

小提琴here

这是有效的,因为如果||为零,localeCompare将仅考虑第二个组件。

我还想指出||不仅简化了对语句的检查,而且还允许您对排序要求进行进一步检查。例如,让我们说你也希望按第三种方式排序(比如行动)。您只需添加另一个||,然后再进行检查即可。

答案 2 :(得分:2)

此替代方法首先比较用户名(比沉淀的localeCompare执行更快),如果它们相等则执行整数之间的公共比较。

&#13;
&#13;
const obj = [{    "timestamp": 1487184625,    "user": "Eric",    "action": "navigate"  },  {    "timestamp": 1487184655,    "user": "Bill",    "action": "browse"  },  {    "timestamp": 1487184685,    "user": "Eric",    "action": "key press"  },  {    "timestamp": 1487184715,    "user": "John",    "action": "idle"  },  {    "timestamp": 1487184755,    "user": "Tran",    "action": "search"  },  {    "timestamp": 1487098049,    "user": "Tran",    "action": "click"  },  {    "timestamp": 1487098079,    "user": "Eric",    "action": "click"  },  {    "timestamp": 1487098109,    "user": "Tran",    "action": "click"  },  {    "timestamp": 1487098139,    "user": "Bill",    "action": "navigate"  },  {    "timestamp": 1487184625,    "user": "Eric",    "action": "navigate"  },  {    "timestamp": 1487184655,    "user": "Bill",    "action": "browse"  },  {    "timestamp": 1487184685,    "user": "Eric",    "action": "key press"  },  {    "timestamp": 1487184715,    "user": "John",    "action": "idle"  },  {    "timestamp": 1487184755,    "user": "Tran",    "action": "search"  },  {    "timestamp": 1487098049,    "user": "Tran",    "action": "click"  },  {    "timestamp": 1487098079,    "user": "Eric",    "action": "click"  },  {    "timestamp": 1487098109,    "user": "Tran",    "action": "click"  },  {    "timestamp": 1487098139,    "user": "Bill",    "action": "navigate"  }];

const sorted = obj.sort((a, b) => {
  if (a.user === b.user) return a.timestamp - b.timestamp;
  return a.user.localeCompare(b.user);
})

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

答案 3 :(得分:0)

else if(a.timestamp === b.timestamp){
    // compare usernames
    if ( a.username > b.username){
        return 1
    } 
    // etc
}

你快到了,你只需要再添加一个条件案例。如果时间相同,那么您就有机会按次要维度排序(反之亦然)