Reputation: 105449
I have two functions:
array_sort(array, compare_function)
compare_values(value1, value2, case_insensitive)
The function 'array_sort' uses 'compare_values' function to compare values and passes only two values to it, while the 3rd parameter 'case_insensitive' should be passed when using the function 'array_sort'. The only way I know how to do that is to wrap 'compare_values' function into another function:
var my_array = [];
var case_insensitive = true;
var wrapper = function(value1, value2) {
compare_values(value1, value2, case_insensitive)
}
array_sort(my_array, wrapper);
Is there any other way I can do that?
Upvotes: 0
Views: 31
Reputation: 664307
You can write another function to abstract over the wrapper:
function makeWrapper(case_insensitive) {
return function wrapper(value1, value2) {
compare_values(value1, value2, case_insensitive)
};
}
array_sort(my_array, makeWrapper(true));
Upvotes: 1