代码之家  ›  专栏  ›  技术社区  ›  James Delaney

如何排序用户表首先可见的用户应该是活动的,然后是不活动的?

  •  1
  • James Delaney  · 技术社区  · 7 年前

    活动用户应排序在非活动用户之上。

    我正在努力做到这一点 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",
     }, 
    ]
    

    https://codepen.io/nikolatrajkovicq/pen/pGXdpM?editors=1112

    欢迎任何告别。

    2 回复  |  直到 7 年前
        1
  •  19
  •   adiga    6 年前

    你可以用 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)

    只需减去 compareFunction . 这是因为强迫

    true - false === 1
    false - true === -1
    true - true === 0
    
        2
  •  4
  •   Code Maniac    7 年前

    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)