我在表中列出了用户列表。活跃用户应排列在非活跃用户之上。
我正在尝试做这个sort
使用洛达什sortBy
功能,但不成功。
这是如何userArray
looks:
const userArray [
{
// I need to show users which have disabled = false first
// and then users with disabled = true
disabled: true, // <==========
email: "[email protected] /cdn-cgi/l/email-protection",
firstName: "Harriet",
lastName: "Gaither",
role: "claimsHandlerSupervisor",
userId: "03VFpxtMWgY1jKDHDLcrWSw1qzx1",
},
{
disabled: false, // <===========
email: "[email protected] /cdn-cgi/l/email-protection",
firstName: "Harriet",
lastName: "Gaither",
role: "claimsHandlerSupervisor",
userId: "03VFpxtMWgY1jKDHDLcrWSw1qzx1",
},
]
这是带有用户数组和 sortBy lodash 函数的代码的代码笔:https://codepen.io/nikolatrajkovicq/pen/pGXdpM?editors=1112 https://codepen.io/nikolatrajkovicq/pen/pGXdpM?editors=1112
欢迎任何建议。
您可以使用sort https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort像这样:
const userArray=[{disabled:true,email:"[email protected] /cdn-cgi/l/email-protection",firstName:"Harriet",lastName:"Gaither",role:"claimsHandlerSupervisor",userId:"03VFpxtMWgY1jKDHDLcrWSw1qzx1",},{disabled:false,email:"[email protected] /cdn-cgi/l/email-protection",firstName:"Harriet",lastName:"Gaither",role:"claimsHandlerSupervisor",userId:"03VFpxtMWgY1jKDHDLcrWSw1qzx1",},]
userArray.sort((a,b) => a.disabled - b.disabled)
console.log(userArray)
您只需减去里面的布尔属性即可compareFunction
。这是因为强制而有效
true - false === 1
false - true === -1
true - true === 0
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)