js secondary sort by different column depending on id of equal column
by aaronk85
JavaScript
var workers = [
{"id":"111","name":"April", "most":"7","sd-111222":"4","sd-111333":"2","sd-111444":"2","sd-111555":"2"},
{"id":"222","name":"Bruno", "most":"7","sd-111222":"4","sd-222333":"1","sd-222444":"2","sd-222555":"2"},
{"id":"333","name":"Carlos","most":"6","sd-111333":"1","sd-222333":"2","sd-333444":"2","sd-333555":"1"},
{"id":"444","name":"Dwayne","most":"5","sd-111444":"1","sd-222444":"1","sd-333444":"1","sd-444555":"2"},
{"id":"555","name":"Ed", "most":"5","sd-111555":"1","sd-222555":"1","sd-333555":"2","sd-444555":"3"}
];
workers.sort(function(a,b) {
if (a.most == b.most) {
return (b["sd-" + a.id + b.id] - a["sd-" + a.id + b.id])
}
else if (b["sd-" + a.id + b.id] == a["sd-" + a.id + b.id]) {
return (b.id - a.id);
}
else {
return (b.most - a.most);
}
});
workers.forEach(function(elm){
console.log(elm["id"] + " " +elm["name"]);
});