Javascript 按两个条件对数据进行排序?
我有一个JSON对象,我试图按时间和用户名字母顺序对其进行排序: 样本数据:Javascript 按两个条件对数据进行排序?,javascript,arrays,json,Javascript,Arrays,Json,我有一个JSON对象,我试图按时间和用户名字母顺序对其进行排序: 样本数据: const obj = [ {"timestamp": 1487184625, "user": "Eric", "action": "navigate"}, {"timestamp": 1487184655, "user": "Bill", "action": "browse"}, {"timestamp": 1487184685, "user": "Eric", "action": "key p
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
}
}))
console.log(对象排序((a,b)=>{
//if(a.user.localeCompare(b.user)){
//返回1
// }
//如果(!a.user.localeCompare(b.user)){
//返回-1
// }
如果(a.时间戳>b.时间戳){
返回-1
}
if(a.时间戳
但我似乎无法让它工作。我也希望避免使用Lodash。有什么建议吗
else if(a.timestamp === b.timestamp){
// compare usernames
if ( a.username > b.username){
return 1
}
// etc
}
您就快到了,您只需要添加一个以上的条件。如果时间相同,则您将有机会按第二维度进行排序(反之亦然)
您就快到了,您只需要添加一个以上的条件。如果时间相同,则您有机会按第二维度进行排序(反之亦然)检查属性
user
上的localeCompare
的结果,如果是-1
或1
,则返回该结果。否则(结果为0
),返回时间戳的比较:
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
),则将返回|
的另一个操作数的值(按时间戳排序)检查属性用户
上的本地比较
的结果,如果它是-1
或1
,则返回该结果。否则(结果为0
),返回时间戳的比较:
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
),则将返回|
的另一个操作数的值(按时间戳进行排序)这可以通过单个排序函数行解决:
var results = obj.sort((a, b) => {
return a.user.localeCompare(b.user) || b.timestamp - a.timestamp
})
拨弄
这是因为,如果<>代码> LoalEclipse < /C> > 0,则代码> > <代码>将只考虑第二个组件。< /P>
我还想指出,|
不仅使语句的检查变得简洁,还允许您在排序要求中链接进一步的检查。例如,假设您还希望按照第三个选项(比如action)进行排序。您只需添加另一个|
,并进一步检查即可。这可以通过单个排序功能行解决:
var results = obj.sort((a, b) => {
return a.user.localeCompare(b.user) || b.timestamp - a.timestamp
})
拨弄
这是因为,如果<>代码> LoalEclipse < /C> > 0,则代码> > <代码>将只考虑第二个组件。< /P>
我还想指出,|
不仅使语句的检查变得简洁,还允许您在排序要求中链接进一步的检查。例如,假设您还希望按照第三个选项(比如action)进行排序。您只需添加另一个|
,并进一步检查即可。此备选方案首先比较用户名(比沉淀本地比较执行更快),如果它们相等,则执行整数之间的通用比较
const obj=[{“时间戳”:1487184625,“用户”:“埃里克”,“动作”:“导航”},{“时间戳”:1487184655,“用户”:“账单”,“动作”:“浏览”},{“时间戳”:1487184685,“用户”:“埃里克”,“动作”:“按键”},{“时间戳”:1487184715,“用户”:“约翰”,“动作”:“空闲”},{“时间戳”:1487184755,“用户”:“交易”,“行动”:“搜索”},{“时间戳”:1487098049,“用户”:“交易”,“行动”:“点击”},{“时间戳”:1487098079,“用户”:“埃里克”,“行动”:“点击”},{“时间戳”:1487098109,“用户”:“交易”,“行动”:“点击”},{“时间戳”:1487098139,“用户”:“账单”,“行动”:“导航”},{“时间戳”:1487184625,“用户”:“埃里克”,“动作”:“导航”},{“时间戳”:1487184655,“用户”:“账单”,“动作”:“浏览”},{“时间戳”:1487184685,“用户”:“埃里克”,“动作”:“按键”},{“时间戳”:1487184715,“用户”:“约翰”,“动作”:“空闲”},{“时间戳”“:1487184755,“用户”:“交易”,“行动”:“搜索”},{“时间戳”:1487098049,“用户”:“交易”,“行动”:“点击”},{“时间戳”:1487098079,“用户”:“埃里克”,“行动”:“点击”},{“时间戳”:1487098109,“用户”:“交易”,“行动”:“点击”},{“时间戳”:1487098139,“用户”:“账单”,“行动”:”“导航”}];
常量排序=对象排序((a,b)=>{
如果(a.user===b.user)返回a.timestamp-b.timestamp;
返回a.user.localeCompare(b.user);
})
console.log(已排序);
.as console wrapper{max height:100%!important;top:0;}
此替代方案首先比较用户名(比沉淀本地比较执行更快),如果它们相等,则执行整数之间的公共比较
const obj=[{“时间戳”:1487184625,“用户”:“埃里克”,“动作”:“导航”},{“时间戳”:1487184655,“用户”:“账单”,“动作”:“浏览”},{“时间戳”:1487184685,“用户”:“埃里克”,“动作”:“按键”},{“时间戳”:1487184715,“用户”:“约翰”,“动作”:“空闲”},{“时间戳”“:1487184755,“用户”:“传输”,“操作”:“搜索”},{“时间戳”:1487098049,“用户”:“传输”,“操作”:“单击”},{“时间戳”:1487098079,“用户”:“埃里克”,