I need to write a common sort function. I am using jQuery for sorting. jQuery sort function only accepts two parameters as input. But I want to pass another parameter to that function. How can I do that?
Something like this:
obj.sort(StringSort);
obj2.sort(StringSort);
function StringSort(a, b, desc)
{
var aText = $(a).attr(desc).toLowerCase();
var bText = $(b).attr(desc).toLowerCase();
if(aText == bText)
return 0;
return aText > bText ? 1 : -1;
}
You can create a function that returns a function. The outer function accepts the additional argument and you use the returned function as sorting callback:
The inner function has access to all parameters of the outer function since it is a closure [MDN].