How can I pass an argument to callback method while calling it from arrayobj.sort(sortFunction) method.I want to pass "sortOrder" to "Compare" as well to sort it accending or desecding.
var sortOrder = "asc";
var a = new Array(4, 11, 2, 10, 3, 1);
b = a.sort(Compare); // how to pass "sortOrder" here
// Sorts array elements in ascending order numerically.
function Compare(first, second, sortOrder) // here sortOrder require
{
if(sortOrder == "asc"){
if (first == second)
return 0;
if (first < second)
return -1;
else
return 1;
}
else{
if (first == second)
return 0;
if (first < second)
return 1;
else
return -1;
}
}
}
a.sort((p,c) => compare(p,c))