javascript

Javascript Sort() By Multiple Criteria

const teams = [
    {name: "Aviso1", state: true, priority: 1},
    {name: "aviso3", state: true, priority: 3},
    {name: "Aviso2", state: false, priority: 2},
    {name: "aviso4", state: true, priority: 4},
    {name: "aviso5", state: false, priority: 5},
    {name: "aviso6", state: true, priority: 6}
]
let formated = teams.sort((a, b) => a.state === b.state ?  a.priority - b.priority : b.state - a.state);

console.log(formated)


/*
[
  { name: 'Aviso1', state: true, priority: 1 },
  { name: 'aviso3', state: true, priority: 3 },
  { name: 'aviso4', state: true, priority: 4 },
  { name: 'aviso6', state: true, priority: 6 },
  { name: 'Aviso2', state: false, priority: 2 },
  { name: 'aviso5', state: false, priority: 5 }
]*/
Was this helpful?