Can someone explain this?

This is from JS course from Arrays section #12:

const courses = [
{id: 1, name: ‘Node.js’},
{id: 2, name: ‘JavaScript’}
];

courses.sort(function(a,b){
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
});

My question is, where is the function getting its parameter from? Can someone please elaborate the function? Its driving me crazy. Thanks.

You will find Mosh’s explanation easier if you read the mdn documentation on Array.prototype.sort.

One way to write Array.sort in Js is like I’ve done below"
sort(function compareFn(a, b) { … })

The compareFn takes parameters a and b which represent the individual items in the array we’d like to sort.

Parameter “a” is the first element for comparison.
Parameter “b” is the second element for comparison.

It will also help if you log a and b within your compareFn. It will make things clearer.

Let me know if you need further clarification.

1 Like

What if there are more than 2 elements to sort. Do we add more parameters to represent the other elements?

No, that is not how the sorting function is being used. The sorting function is applied to any two elements that need to be compared.

Imagine you had to sort the elements manually, you are using this function only to compare elements to decide which one goes before the other.

To use a simpler case, let us consider sorting numbers and let’s extract the function into a variable:

const nums = [ 5, 2, 1, 3, 4 ];
const compareNums = function(a, b) { return a - b; };
nums.sort(compareNums);

The specification does not tell you specifically how the sort is implemented (only that it is in-place). Tracing through a basic min-sorting algorithm (basically starting at the first index and finding the next smallest number in the array to put there).

  • First iteration (index = 0):
    • grab the first two numbers and use compareNums to find out which is smaller and put it at the index => compareNums(5, 2) = 3 therefore we know 2 is less so we swap them; array = [2, 5, 1, 3, 4]
    • compare current min with the next number => compareNums(2, 1) = 1 therefore we know 1 is less so we swap them; array = [1, 5, 2, 3, 4]
    • compare current min with the next number => compareNums(1, 3) = -2 therefore we know 1 is less (no swap needed)
    • compare current min with the next number => compareNums(1, 4) = -3 therefore we know 1 is less (no swap needed); we are at the end of the array so we are done with this iteration
  • Iteration 2 (index = 1, array = [1, 5, 2, 3, 4])
    • starting at the index, grab the next two values and compare => compareNums(5, 2) = 3 so we know 2 is less and we swap; array = [1, 2, 5, 3, 4]
    • compare current min with the next number => compareNums(2, 3) = -1 so we know 2 is less (no swap needed);
    • etc, we get to the end of the array again with no further modifications and we are done with this iteration
  • Iteration 3 (index = 2, array = [1, 2, 5, 3, 4])
    • starting at the index, grab the next two values and compare => compareNums(5, 3) = 2 so we know 3 is less and we swap; array = [1, 2, 3, 5, 4]
    • etc, we get to the end of the array again with no further modifications and we are done with this iteration
  • Iteration 3 (index = 2, array = [1, 2, 3, 5, 4])
    • starting at the index, grab the next two values and compare => compareNums(5, 4) = 1 so we know 4 is less and we swap; array = [1, 2, 3, 4, 5]

As you can see, we now have a sorted array (in place). Hopefully this clarifies how the function can be used repeatedly to do the sorting.