I'm attempting to sort items first by their values and then alphabetically if they have the same value.
function order(a, b, total) {
if (total == 0) {
return a.localeCompare(b)
} else {
return total;
}
}
var thingsArr = {"lamp":2, "books":2};
Object.keys(thingsArr).sort(function (a, b) {
order(a, b, thingsArr[b] - thingsArr[a])
});
Why doesn't the else
statement inside the function order handle alphabetical sorting of the resulting array when the if
part handles sorting based on highest value? It only works correctly when used separately. What am I missing here?