That' not how the comparison function for Array.sort()
works at all: it should return negative, zero, or positive.
Eating up all or part of the props
argument (due to your array.shift()
call) is bad practice.
The usage is cumbersome:
array.sort(function(a,b) { return comparePropsOfAWithB(a,b,["a","b","c"]) })
Wouldn't it be nicer to write a propertyComparator(…)
function that returns a function?
array.sort(propertyComparator('a', 'b', 'c'))
Something like this:
function propertyComparator() {
var props = arguments;
return function(a, b) {
for (var i = 0; i < props.length; i++) {
var aProp = a[props[i]];
var bProp = b[props[i]];
if (aProp < bProp) return -1;
if (aProp > bProp) return +1;
}
return 0;
};
}