I was wondering if it is possible to pass a named function to Array.sort (). I am trying to do something like logic below, but obviously he complains that it is anot defined.
if (iWantToSortAscending) {
myArray.sort(sortAscending(a, b));
} else {
myArray.sort(sortDescending(a, b));
}
function sortAscending(a, b) {
...
sorty worty
...
}
function sortDescending(a, b) {
...
sorty worty differently
...
}
Obviously, I could make the code work as shown below, and it is, but I would prefer not to use anonymous functions for readability and debugging.
if (iWantToSortAscending) {
myArray.sort(function (a, b) {
...
sorty worty
....
}
} else {
...
source
share