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?
Similar Posts
- Sort array using Array.sort() in javascript
- Sort array of objects by key value in Javascript
- Compressed String with Sort - C3 AI - CRM - Full Stack - 11/11/21
- Sort Tree - Bloomberg - Port Interactive Analytics - Front End - 12/6/21
- JavaScript ES6 filtering object with multiple params
- Concatenate multiple objects into one using Javascript
- Pass multiple arguments when calling a function which takes single argument Javascript