Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
let services = [
{
crit: 5,
warn: 3,
pass: 7
},
{
crit: 4,
warn: 4,
pass: 2
},
{
crit: 4,
warn: 8,
pass: 5
},
{
crit: 8,
warn: 2,
pass: 11
},
{
crit: 9,
warn: 3,
pass: 7
}
];
console.log(services.sort((serviceA, serviceB) => {
let order = 'asc';
let params = ['crit', 'warn', 'pass'];
let a = serviceA, b = serviceB;
if (order === 'desc') {
a = serviceB;
b = serviceA;
params = params.reverse();
}
for (i in params) {
let param = params[i];
if (a[param] === b[param]) continue;
return a[param] > b[param] ? -1 : 1;
}
return 0;
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment