按两个条件对数据进行排序?

编程入门 行业动态 更新时间:2024-10-26 18:20:12
本文介绍了按两个条件对数据进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个JSON对象,我想按时间和用户名的字母顺序对其进行排序:

I have a JSON object and I'm trying to sort it by both time and username alphabetically:

样本数据:

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.有什么建议吗?

But I can't seem to get it working. I'd like to avoid using Lodash as well. Any suggestions?

推荐答案

在属性user上检查localeCompare的结果,如果它是-1或1,则返回该结果.否则(结果为0),返回timestamp的比较:

Check the result of localeCompare on the property user, if it is either -1 or 1 then return that result. Otherwise (result is 0), return the comparaison of 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返回真实值(即 -1或1),则将返回该真实值,而||的其余部分将被忽略.如果返回假值(即 0),则将返回||的另一个操作数的值(按timestamp排序)

if localeCompare returned a truthy value (i.e. -1 or 1), then that truthy value will be returned and the rest of || will be ignored. If it returned a falsy value (i.e. 0) then the value of the other operand of || will be returned (which is sorting by timestamp)

更多推荐

按两个条件对数据进行排序?

本文发布于:2023-10-30 12:10:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1542809.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件   两个   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!