The comparator function is called with the underscore _.sortBy
with an array element as an argument, there is a ton of object creation and iteration for what essentially boils down to a much faster native sort:
someArrayOfTasks.sort( function( taskA, taskB ) { return taskA.dueDate < taskB.dueDate ? -1 : taskA.dueDate > taskB.dueDate ? 1 : 0; });
Or a simpler example:
[3,2,5,1,4].sort( function(a,b){ return ab; });
Here, the comparator function gets its arguments from the sort function when trying to resolve the sort order, the value that you return from the comparator determines in which order the array is sorted.
I would suggest normal sorting by cellular network, since it is 10 times faster for me in chrome and easier to understand, because the comparator function is actually ... compares: http://jsperf.com/underscore-sort-vs- normal-sort
source share