Confused about sorting numbers. Stat Calc step 21

in stat calc step 21, it says:

Summary

Step 21 Passed

By default, the .sort() method converts the elements of an array into strings, then sorts them alphabetically. The .sort() method mutates the original array. This works well for strings, but not so well for numbers. For example, 10 comes before 2 when sorted as strings, but 2 comes before 10 when sorted as numbers.

To fix this, you can pass in a callback function to the .sort() method. This function takes two arguments, which represent the two elements being compared. The function should return a value less than 0 if the first element should come before the second element, a value greater than 0 if the first element should come after the second element, and 0 if the two elements should remain in their current positions.

To sort your numbers from smallest to largest, pass a callback function that takes parameters a and b, and returns the result of subtracting b from a.

The answer to this lesson is: const sorted = array.sort((a, b) => a - b) and I don’t know why that is or what it means. I thought the answer would look close to this:

const sorted = array.sort((a, b) => {
 if (a<b){
      return -1
    } 
    else if (a>b){
      return 1
    }
    else
    return 0 }

and I wasn’t sure where to put the a-b in all of that. But what I am really confused about is why all that logic is not in the correct answer at all? It makes me confused why is it telling me to use that logic in the second paragraph.

I’m also confused as to WHY we are supposed to subtract b from a

Also, why are there only 2 parameters to the sort function, and what do they represent? Because our array can have more than 2 values.

Thanks for your help!

Hi @totaleclipse

By default, the .sort() method converts the elements of an array into strings, then sorts them alphabetically. The .sort() method mutates the original array. This works well for strings, but not so well for numbers. For example, 10 comes before 2 when sorted as strings, but 2 comes before 10 when sorted as numbers.

For this step, you learn that the default action of sort without any parameters is to alphabetically sort an array, after converting the elements into strings.

To sort your numbers from smallest to largest, pass a callback function that takes parameters a and b, and returns the result of subtracting b from a.

a is a placeholder for an element, and b is a placeholder for the next element in the array.

For this step you need to return the result of subtracting the parameter b from a.

For the first two elements of the array, if the value is negative then a is lower than b, so a remains in the same position in the array, then the comparison moves to the second (a) and third (b) elements of the array.

If the return value is zero, a and b are the same so they do not change their position in the array.

If the return value is positive, then a is larger then b, so their positions are swapped.

This iteration continues until all elements in the array are sorted from lowest to highest.

Happy coding

1 Like

Thanks, Teller. I think I understand now. So the function I wrote, that is already baked into the .sort function? Plus logic that tells it to swap or not swap depending on it returning positive or negative results. Right?

In JavaScript the following array is ordered from lowest to highest

["1", "23", "3", "700", "9"]

because they are strings.

Using a minus sign in the callback function forces JavaScript to convert the elements into numbers.

console.log("10" - "3") \\ 7

So a negative return value indicates a value is lower than the next one, or a zero means the values are the same, so the position stays the same.

If it is a negative return value, then the positions are swapped.

Yes, the .sort() method has logic in the background which converts elements to strings then sorts them alphabetically.

To sort numbers, you need a call back function which converts the strings into numbers, and make a comparison.

Happy coding

1 Like