How does Javascript’s sort() work?

Is the array sort callback function called many times during the course of the sort?

Yes

If so, I’d like to know which two numbers are passed into the function each time

You could find out your self with:

array.sort((a,b) => {
  console.log(`comparing ${a},${b}`);
  return a > b ? 1
               : a === b ? 0 
                         : -1;
});

EDIT

This is the output I’ve got:

25,8
25,7
8,7
25,41

Leave a Comment