"Sort Arrays with .sort" HELP!

This is in reference to the “Sort Arrays with .sort” challenge…

I was able to pass this challenge (I basically had to do the opposite of what was shown in the example)- but I don’t understand what a and b are- can anyone explain?

Below is the function solved:

var array = [1, 12, 21, 2];

// Only change code below this line.

array.sort(function (a,b){
  return b-a;
});

Thanks!

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

.sort needs a callback function to determine how to sort the array. From MDN:

If compareFunction is supplied, the array elements are sorted according to the return value of the compare function. If a and b are two elements being compared, then:

  • If compareFunction(a, b) is less than 0, sort a to a lower index than b, i.e. a comes first.
  • If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.
  • If compareFunction(a, b) is greater than 0, sort b to a lower index than a.
  • compareFunction(a, b) must always return the same value when given a specific pair of elements a and b as its two arguments. If inconsistent results are returned then the sort order is undefined.
2 Likes

Thanks Kev! This was my fault for not seeing the link to the MDN page in the challenge’s direction- thanks for the help with the challenge though- and thanks for correcting the question format!

Thanks for detailed explanation on how sort works wrt array of number.

But I’m not pretty clear with callback function, if you can share some explanation it will be of great help.

Thanks,
vikram

Sometimes a function will need another function passed to it so it can do its work. That function that you passed is usually called a callback function.

The sort function has a default way of sorting array values, but if you want to change how it sorts the values (as is usually the case), you’ll need to provide a callback function that describes how to sort (as documented above).

Thanks for detailed clarification.

Regards,
Vikram

Hello @kevcomedia, thanks for the explanation, but i would like to go a bit deeper in the mechanics of the callback function.

Lets say you have array [1,3,5,4,2] and you use the callback function to compare the values. When callback functions runs, does it compare 1 to 3, then 3 to 5? I’m trying to get the sense on how a and b get compared.

Hope my question is clear :slight_smile:

The number of actual comparisons sort() has to do, and which values it actually compares, depends on the algorithm it is using underneath the hood. Also, I believe that sort() may use different algorithms depending on the number of values you need to sort. And the algorithms being used could be different depending on the browser you are using. You can google ‘sorting algorithms’ if you want to know how the most popular ones work.

@camperextraordinaire That’s interesting. My browser (chrome) gives

comparing a = 12 to b = 1
comparing a = 21 to b = 12
comparing a = 2 to b = 21
comparing a = 2 to b = 12
comparing a = 2 to b = 1

Must be a different implementation.

@camperextraordinaire I think something might be wrong with your output. Where did the other 1 come from?