Understanding number sorting

Hi I’m currently looking at sorting using functions and have come across this code:

 function numberSort(a,b){
     if(a < b){
       return -1;
     }else if (a > b){
       return 1;
       return 0;
 var values = [4,3,2];

I understand the lexicographical order part. I cant understand how the numbers are actually entered into the function to compare them.

for example will 2, 3 (a,b) get compared first then 2, 4. Then after 3 and 4?

Sorry if ive made this confusing trying to understand it.

Also when I have consoled.log the parameters(a,b) why does the first number in the array equal the b parameter and not a? In the example above a = 3 and b = 4 when i consoled logged them?

@05beckga when you post code in the forum, you need to format it. To do so you can either select all the code and click the </> button, or type 3 backticks ``` on a separate line both before and after the code block.

I have done it for you this time.

1 Like

There isn’t supposed to be any rhyme or reason as to the order that the numbers get compared.
The important thing is that less than is given -1, equal is 0, and greater is +1

By the way, you can just use a-b to compare numbers, for less than zero means the same as -1, and greater than zero means the same as +1.

var numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
  return a - b;
console.log(numbers); // [1, 2, 3, 4, 5]

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

1 Like

Thanks for you reply.

Where i’m also getting confused is how does the return give me the answer. How does that affect the array to put it in order.

Steps of the output:

 var values = [4,3,2]; // array
 values.sort(numberSort); // sorts the array in lex order [2, 3, 4] then calls the function

what does the -1, 1, 0 return to create the correct order?

Maybe i’m trying to dig to deep but any help would be much appreciated. Thanks

once the comparisons are done what does the 1, -1 and 0 actually do to contribute to the answer and put it in order?

sort says “Sort the array using this function. An execution of the function tells me either:
<0 : Put A before B.
0: Don’t change their order.
>0: Put B before A.
I will continue until all possible combinations of A and B result in the same array (meaning it’s sorted).”

Because it’s a function, it needs to return that -1,0,or 1, to the sort function, so that it knows what to do with the elements of the array that it was called with.

Think of it this way. You’re going to write the world’s greatest sorting algorithm that will go into the next JavaScript release. And you want it to be able to sort any array, any array, because that would be the most flexible thing. So some smart ass in the testing department tells you he’s wanting to sort an array of apples, oranges and cherries. Some other guy tells you he wants to be able to sort colors, and other says he wants to be able to sort moods, so happy is greater than sad.

You pause, realizing that you were thinking that folks only sorted numbers, and that they key magic of your sorting algorithm was its speed in traversing the list of items to sort. You hadn’t even thought of sorting fruits and moods. But after thinking for a bit, you make this key enhancement to your sorting algorithm: Instead of just comparing “is a > b” in your code, you will require the user to provide you a small piece of code that compares two things and tells you what order they should be in. In that way, you relinquish responsibility of making the comparison, and if you pass the *user’s code" two moods, or two fruits, the user’s code will resolve how they sort. You can then move forward with your speedy sorting algorithm, calling this little routine every time two items need to be compared, and with the satisfaction that not only can you have a fast sort algorithm, but that you can now sort any type of object without needing to know about that object in advance…

So that little routine of course is the NumberSort code you’re referring to, and returning -1, 0 or 1 is just the convention the person who wrote the sort algorithm chose as return codes for the comparison.