I have a function that sorts an array and within it, I have custom sort functions. Something like this:
function SortTheArray() {
function SortCriteria1Asc(a, b) { ... }
function SortCriteria1Dsc(a, b) { ... }
function SortCriteria2Asc(a, b) { ... }
function SortCriteria1Asc(a, b) { ... }
var CustomSort;
switch (SomeVar) {
case 1:
CustomSort = SortCriteria1Asc;
break;
case 2:
CustomSort = SortCriteria1Dsc;
break;
case ....
}
SomeDataArray.sort(CustomSort);
}
Is it possible to remove the switch statement and say that the CustomSort function is simply equal to the nth nested function?
Thanks for your suggestions.
Don’t give the function names, store them in an array.
Then just:
… but numerically indexed function identifiers don’t sound like a very good idea. You would probably be better off using an object rather than array and giving them descriptive names.