I'm working on a function to sort two elements using several properties. I want to know if the function is well structured.
function comparePropsOfAWithB(a, b, props) {
var prop = props[0];
var propA = a[prop];
var propB = b[prop];
if (propA === propB) {
var array = props;
array.shift();
return props.length ? comparePropsOfAWithB(a, b, props) : false
} else if (propA < propB ) {
return true
} else {
return false
}
}
This function is meant to be used alongside the sort method like this:
array.sort(function(a,b) { return comparePropsOfAWithB(a,b,["a","b","c"]) })
The intent of the function is to compare two objects using several values to determine if they are different. In the example, the key "a
" will be tested, and if it fails, "b
" will be tested until the last element of the array is reached.