我在表中列出了用户列表。 活动用户应在非活动用户上方排序。
我正在尝试使用lodash sort
函数来制作此sortBy
,但未成功。
userArray
的外观如下:
const userArray [
{
// I need to show users which have disabled = false first
// and then users with disabled = true
disabled: true, // <==========
email: "hgaither@cmregent.com",
firstName: "Harriet",
lastName: "Gaither",
role: "claimsHandlerSupervisor",
userId: "03VFpxtMWgY1jKDHDLcrWSw1qzx1",
},
{
disabled: false, // <===========
email: "hgaither@cmregent.com",
firstName: "Harriet",
lastName: "Gaither",
role: "claimsHandlerSupervisor",
userId: "03VFpxtMWgY1jKDHDLcrWSw1qzx1",
},
]
这是带有用户数组和sortBy loadsh函数代码的代码笔: https://codepen.io/nikolatrajkovicq/pen/pGXdpM?editors=1112
任何人都欢迎。
答案 0 :(得分:5)
您可以像这样使用sort
:
const userArray=[{disabled:true,email:"hgaither@cmregent.com",firstName:"Harriet",lastName:"Gaither",role:"claimsHandlerSupervisor",userId:"03VFpxtMWgY1jKDHDLcrWSw1qzx1",},{disabled:false,email:"hgaither@cmregent.com",firstName:"Harriet",lastName:"Gaither",role:"claimsHandlerSupervisor",userId:"03VFpxtMWgY1jKDHDLcrWSw1qzx1",},]
userArray.sort((a,b) => a.disabled - b.disabled)
console.log(userArray)
之所以有效,是因为
false - true === -1
答案 1 :(得分:4)
您可以使用sort
const userArray = [{disabled:true,email:"hgaither@cmregent.com",firstName:"Harriet",lastName:"Gaither",role:"claimsHandlerSupervisor",userId:"03VFpxtMWgY1jKDHDLcrWSw1qzx1",},{disabled:false,email:"hgaither@cmregent.com",firstName:"Harriet",lastName:"Gaither",role:"claimsHandlerSupervisor",userId:"03VFpxtMWgY1jKDHDLcrWSw1qzx1",},{disabled:true,email:"hgither@cmregent.com",firstName:"Hrriet",lastName:"Gither",role:"claisHandlerSupervisor",userId:"0VFpxtMWgY1jKDHDLcrWSw1qzx1",},]
let op = userArray.sort(({disabled:A}, {disabled:B})=> A-B)
console.log(op)